MvvmLight EventToCommand and WPFToolkit DataGrid double-click

Thomas picture Thomas · Dec 10, 2009 · Viewed 11.5k times · Source

Trying to figure out how to use EventToCommand to set a datagrid double click handler for rows. The command lives in the viewmodel for each row. Just that much out of my experience, since I haven't used interactions yet.

Thanks.

I would have used mvvmlight tag, but I don't have high enough rep yet to make new tags.

Answer

CodeWeasel picture CodeWeasel · Apr 23, 2010

This would be the solution if the Command lives on the "GridVieModel" and not on the "RowViewModel".

    <Window...    
         ...xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
            xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
            xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras">
         <dg:DataGrid x:Name="dg">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding SelectedItem, ElementName=dg}" Command="{Binding Path=SelectCommand, Mode=OneWay}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
            </dg:DataGrid>
    </Window>

You could create a rowview since the row also has its own viewmodel and use the mousedoubleclick event of a child element of the row (container) in the rowview.

Or you create a converter for your command binding:

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SelectedItem, ElementName=dg, Mode=OneWay, Converter=...}"/>

The converter then would check if the selectedItem is of the required type to return the command (Something like ISelectCommandable with a RelayCommand Property)