This is a migrated thread and some comments may be shown as answers.

How to raise an event when select a row?

8 Answers 647 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ardmore
Top achievements
Rank 1
ardmore asked on 05 Jul 2016, 03:27 PM

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?

8 Answers, 1 is accepted

Sort by
0
Ravin
Top achievements
Rank 1
answered on 06 Jul 2016, 08:18 PM

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

0
ardmore
Top achievements
Rank 1
answered on 06 Jul 2016, 09:04 PM

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.

0
Ravin
Top achievements
Rank 1
answered on 06 Jul 2016, 09:33 PM

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

0
ardmore
Top achievements
Rank 1
answered on 07 Jul 2016, 12:09 PM

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.};

0
ardmore
Top achievements
Rank 1
answered on 07 Jul 2016, 12:21 PM

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.

0
Stefan Nenchev
Telerik team
answered on 08 Jul 2016, 11:29 AM
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.
0
ardmore
Top achievements
Rank 1
answered on 08 Jul 2016, 12:52 PM

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?

0
Stefan Nenchev
Telerik team
answered on 12 Jul 2016, 03:25 PM
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.
Tags
GridView
Asked by
ardmore
Top achievements
Rank 1
Answers by
Ravin
Top achievements
Rank 1
ardmore
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or