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

Better Way to Bind to Row Double Click?

1 Answer 1266 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joshua
Top achievements
Rank 2
Joshua asked on 29 Aug 2019, 10:34 PM

I was previously using the Telerik EventToCommandBehavior to capture the double-click event for my GridView, but as others have discovered  I had the issue that it also triggered when a user clicked on the scrollbar quickly:

<telerik:EventToCommandBehavior.EventBindings>
  <telerik:EventBinding EventName="MouseDoubleClick" Command="{Binding ShowEditWindowCmd}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadGridView}}}" />
</telerik:EventToCommandBehavior.EventBindings>

 

So now I've switched over to another solution using the code-behind, but it feels like it breaks the MVVM model too much in my case.  I realize I'm splitting hairs because this does what I want, but is there maybe a better (cleaner) way to do this?  I would love to use the EventToCommandBehavior on the row, but couldn't figure out how.

public partial class PackageBoardsGrid : UserControl
{
    PackageBoardsPage pageVM;
 
    public PackageBoardsGrid()
    {
        InitializeComponent();
        this.DataContextChanged += ( o, e ) =>
        {
            if( e.NewValue is PackageBoardsPage )
                pageVM = (PackageBoardsPage)e.NewValue;
        };
    }
 
    private void Grid_RowLoaded( object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e )
    {
        if( e.Row is GridViewRow ) {
            e.Row.AddHandler(GridViewRow.MouseDoubleClickEvent, new MouseButtonEventHandler(OnRowMouseDoubleClick), true);
        }
    }
 
    private void OnRowMouseDoubleClick( object sender, MouseButtonEventArgs args )
    {
        if( pageVM != null ) {
 
            var gridViewRow = sender as GridViewRow;
            if( gridViewRow != null ) {
                var data = gridViewRow.DataContext as LiveBoard;
                if( data != null )
                    pageVM.ShowEditWindowCmd.Execute(data);
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 03 Sep 2019, 08:28 AM

Hello Joshua,

To achieve your requirement you can use the RowLoaded event and the EventToCommandBehavior in code. Here is an example in code:

private void GridView_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
{
	var eventBindings = EventToCommandBehavior.GetEventBindings(e.Row);
	var eventBinding = new EventBinding();
	eventBinding.EventName = "MouseDoubleClick";
	eventBinding.CommandParameter = e.DataElement;
	BindingOperations.SetBinding(eventBinding, EventBinding.CommandProperty, new Binding("DoubleClickCommand") { Source = this.DataContext });
	eventBindings.Add(eventBinding);
}

I would recommend you also to use the RowUnloaded event in order to remove the EventBinding from the corresponding row. This way you will avoid memory leaks. I attached a sample project showing this approach. Can you please give it a try and let me know how it goes?

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
YBOT
Top achievements
Rank 1
Veteran
commented on 21 Sep 2022, 01:03 PM

I've tried the code above with EventName = "MouseLeftButtonDown" but it never fires. How can I get a single left click handled in my ViewModel?
Martin Ivanov
Telerik team
commented on 21 Sep 2022, 01:29 PM

Some routed events (like MouseLeftButtonDown) can be handled in some controls like the RadGridView one. In order to raise the command in this case you should set the RaiseOnHandledEvents property of the EventBinding to True.

eventBinding.RaiseOnHandledEvents = true;

 

Tags
GridView
Asked by
Joshua
Top achievements
Rank 2
Answers by
Martin Ivanov
Telerik team
Share this question
or