WPF DataGrid: CommandBinding to a double click instead of using Events

前端 未结 4 1257
轮回少年
轮回少年 2021-02-01 02:37

I know how to use the MouseDoubleClick event with my DataGrid to grab the selectedvalue, but how would one go about using command bindings instead? That way my ViewModel can han

4条回答
  •  爱一瞬间的悲伤
    2021-02-01 03:14

    No need for attached behaviors or custom DataGrid subclasses here.

    In your DataGrid, bind ItemsSource to an ICollectionView. The trick here is to set IsSynchronizedWithCurrentItem="True" which means the selected row will be the current item.

    The second part of the trick is to bind CommandParameter to the current item with the forward slash syntax.

    When a row is double clicked, the command will be executed with the clicked row as argument.

    
        
            
        
    
    

    This is how a (simplified) version of the view model would look:

    class MyViewModel
    {
        public ICollectionView CollectionView { get; set; }
    
        public ICommand DoubleClickCommand { get; set; }
    }
    

提交回复
热议问题