Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 handle the logic.

So far I have the following:

<DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="True" MouseDoubleClick="TestGrid_MouseDoubleClick"
          ItemsSource="{Binding Registrations}" SelectedValue="{Binding CurrentRegistration}" IsReadOnly="True" AlternationCount="2" GridLinesVisibility="None">

I want to get rid of MouseDoubleClick and replace it appropriately.

like image 869
myermian Avatar asked Oct 06 '10 20:10

myermian


3 Answers

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.

<DataGrid
    ItemsSource="{Binding CollectionView}"
    IsSynchronizedWithCurrentItem="True">
    <DataGrid.InputBindings>
        <MouseBinding
            MouseAction="LeftDoubleClick"
            Command="{Binding DoubleClickCommand}"
            CommandParameter="{Binding CollectionView/}"/>
    </DataGrid.InputBindings>
</DataGrid>

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

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

    public ICommand DoubleClickCommand { get; set; }
}
like image 168
Mizipzor Avatar answered Oct 29 '22 14:10

Mizipzor


Another solution is to add input bindings, and to bind the selectedItem to a property so you'll know which one was selected:

<DataGrid SelectedItem="{Binding SelectedItem}">
      <DataGrid.InputBindings>
          <MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
     </DataGrid.InputBindings>
</DataGrid>
like image 25
Tamar Cohen Avatar answered Oct 29 '22 16:10

Tamar Cohen


Use this library

Sample binding to datagrid event:

<DataGrid xmlns:command="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
    command:CommandBehavior.Event="MouseDoubleClick"
    command:CommandBehavior.Command="{Binding TestCommand}" />

But this code is better, because raises on row clicks only:

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/>
            <Setter Property="command:CommandBehavior.Command" Value="{Binding DataContext.TestCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/>
        </Style>
    </DataGrid.Resources>
</DataGrid>
like image 3
vortexwolf Avatar answered Oct 29 '22 16:10

vortexwolf