MvvmLight EventToCommand和WPFToolkit DataGrid双击

Tho*_*mas 5 c# mvvm wpftoolkit wpfdatagrid mvvm-light

试图弄清楚如何使用EventToCommand为行设置datagrid双击处理程序.该命令位于每行的viewmodel中.我的经验就是这么多,因为我还没有使用过互动.

谢谢.

我会使用mvvmlight标签,但我还没有足够高的代表来制作新标签.

Cod*_*sel 11

如果命令位于"GridVieModel"而不是"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>
Run Code Online (Sandbox Code Playgroud)

您可以创建一个rowview,因为该行也有自己的viewmodel,并使用rowview中行(容器)的子元素的mousedoubleclick事件.

或者您为命令绑定创建转换器:

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SelectedItem, ElementName=dg, Mode=OneWay, Converter=...}"/>
Run Code Online (Sandbox Code Playgroud)

然后转换器将检查selectedItem是否具有返回命令所需的类型(类似于使用RelayCommand属性的ISelectCommandable)