How to raise an event when select a row?

9 posts, 0 answers
  1. Hui
    Hui avatar
    14 posts
    Member since:
    Jul 2016

    Posted 05 Jul Link to this post

    I am usingRadGridView.

     

    <telerik:RadGridView x:Name="view1" Width="1025" IsReadOnly="True" ValidatesOnDataErrors="None" AutoGenerateColumns="False" IsFilteringAllowed="False" ShowGroupPanel="False" ShowColumnFooters="False" CanUserResizeColumns="False"
                                   CanUserFreezeColumns="False" SelectionMode="Single" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserInsertRows="False" CanUserDeleteRows="False"
                                   CanUserSelect="False" RowIndicatorVisibility="Collapsed" Height="265" ItemsSource="{Binding MyInformation}">
                   <telerik:RadGridView.Columns>

     

    I want to raise an event in ModelView class if I select a row. How to do it?

  2. Ravin
    Ravin avatar
    16 posts
    Member since:
    Jun 2012

    Posted 06 Jul in reply to Hui Link to this post

    You could call SelectionChanged on the RadGridView, and in the code behind of the controller invoke the event on your view model as appropriate.

    I've also put a button in my RadGridViews before that invokes a command from my view models if that helps you at all:

    XAML:

    <telerik:GridViewColumn>
        <telerik:GridViewColumn.CellTemplate>
            <DataTemplate>
                <telerik:RadButton Content="View Chart"
                             Command="{Binding OpenRecordChartCommand, Source={StaticResource viewModel}, Mode=OneWay}"
                             CommandParameter="{Binding}" />
            </DataTemplate>
        </telerik:GridViewColumn.CellTemplate>
    </telerik:GridViewColumn>

     

    C#:

    public class MyViewModel
    {
         
        #region "Commands"
        public ICommand OpenRecordChartCommand
        {
            get;
            set;
        }
     
        private void OnOpenRecordChartCommandExecuted(object obj)
        {
            var record = obj as wcfNamespace.RecordType;
            if (record != null)
            {
                // Update the selected tracking record to this to start an async call back to update the updated tracking record used for the chart.
                this.reloadRecordFromDb(record);
                 
                // Create a RadWindow referencing the tracking record with a chart.
                RadWindow radWindow = new RadWindow()
                {
                    Header = this.GetHeaderForRecord(record),
                    Content = new MyRecordChartControl(record),
                    Opacity = 1.0,
                    WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner
                };
                 
                // Show the RadWindow.
                radWindow.Show();
            }
        }
        #endregion "Commands"
         
        #region "Constructor"
        public MyViewModel()
        {
            if (!IsDesignTime)
            {
                this.OpenRecordChartCommand = new DelegateCommand(OnOpenRecordChartCommandExecuted);
            }
        }
        #endregion "Constructor"
         
    }

     

     

    Thanks,
    Ravin

  3. DevCraft banner
  4. Hui
    Hui avatar
    14 posts
    Member since:
    Jul 2016

    Posted 06 Jul in reply to Ravin Link to this post

    I want to do it in code behind. I have a mouse down event when I click a row with mouse.

     

    01.private void radGridView_RowLoaded(object sender, RowLoadedEventArgs e)
    02.{
    03.     var row = e.Row as GridViewRow;
    04.     if (row != null)
    05.     {
    06.         row.IsCurrent = true;
    07.         row.AddHandler(GridViewRow.MouseLeftButtonDownEvent,
    08.         new MouseButtonEventHandler(GridViewRow_MouseLeftButtonDown), true);
    09.     }
    10.}
    11. 
    12.        private void GridViewRow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    13.        {
    14.            // change row color????
    15.        }
    I want to change the color then. So I thought to raise an event for that. Not sure whether we can do it since we already have an event. Basically the question can be divided two parts. One select row with mouse, two once we get the row, then to raise an event.

    Thanks for help.

  5. Ravin
    Ravin avatar
    16 posts
    Member since:
    Jun 2012

    Posted 06 Jul in reply to Hui Link to this post

    This approach may work, can't really test it myself, as I have other behavior in my grid which is preventing the event from ever firing:

    private void radGridView_RowLoaded(object sender, RowLoadedEventArgs e)
    {
        var row = e.Row as GridViewRow;
        if (row != null)
        {
            //row.IsCurrent = true;
            row.IsHitTestVisible = true;
            row.MouseLeftButtonDown += (handlerSender, handlerArgs) =>
            {
                row.Background = new SolidColorBrush() { Color = Colors.Yellow };
            };
        }
    }

    Let me know if that works for you.

     

    Thanks,
    Ravin

  6. Hui
    Hui avatar
    14 posts
    Member since:
    Jul 2016

    Posted 07 Jul in reply to Ravin Link to this post

    No. It is not working. The code doesn't reach the inside after the mouse left button down.

    1.row.MouseLeftButtonDown += (object s, MouseButtonEventArgs me) =>
    2.{
    3.      row.Background = new SolidColorBrush() { Color = Colors.Green };
    4.};

  7. Hui
    Hui avatar
    14 posts
    Member since:
    Jul 2016

    Posted 07 Jul in reply to Hui Link to this post

    I think that we need to pass additional data (row) to the method.

    private void GridViewRow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                // need row object
            }

    There is a link for that.

    But I am not sure how to apply it.

  8. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 08 Jul Link to this post

    Hello,

    Regarding the original question in the thread - How to raise an event in ModelView class If I select a row - Please check the following article from our documentation - EventToCommandBehavior.

    However, as far as I understand, your requirement is to change the row`s background when the row is selected. The best way to achieve this is to predefine the template of the GridViewRow. You need to modify the Border element named "Background_Selected" in the VisualStates with the desired color. Please, check the following article - Styling Rows.

    You can also achieve the behavior through Style Triggers:

    <Style TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
               <Style.Triggers>
                   <Trigger Property="IsSelected" Value="True">
                       <Setter Property="Background" Value="Green"/>
                   </Trigger>
               </Style.Triggers>
    </Style>

    The recommended approach is modifying the ControlTemplate, though.

    Please update the thread if you have some difficulties.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  9. Hui
    Hui avatar
    14 posts
    Member since:
    Jul 2016

    Posted 08 Jul in reply to Stefan Nenchev Link to this post

    Error: The attachable property 'Triggers' was not found in type 'Style'.

    My application is Silverlight. Microsoft said it didn't support it.

    Any other options?

  10. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    281 posts

    Posted 12 Jul Link to this post

    Hello Hui,

    Please excuse me for the misleading information regarding the Triggers as they are not supported in Silverlight.

    Did you try the other option from my previous reply? I am here providing a project with the desired functionality. Please review it and consider such approach at your end as well.

    Regards,
    Stefan Nenchev
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
DevCraft banner