wpf - 如何将 WPF DataGridRow 事件与 Interaction.Triggers/MVVM EventToCommand 结合使用

标签 wpf mvvm-light wpfdatagrid eventtocommand

MVVMLight EventToCommand 可用于轻松地在 View 模型上触发 ICommand

<DataGrid>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <GalaSoft_MvvmLight_Command:EventToCommand
                Command="{Binding ProductSelectionChangedCommand, Mode=OneWay}    "
                CommandParameter="{Binding SelectedItems, ElementName=gridProducts}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</DataGrid>

在此实例中,SelectionChanged 事件属于 DataGrid ,并且 Interaction.Triggers xaml 直接嵌套在 DataGrid 内。

当事件是 DataGridRow (每行都有自己的 events)时,我不知道如何执行相同的操作。

我设法做到了这一点,但它涉及一个我想避免的处理程序函数:

<DataGrid>
    <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
            <EventSetter Event="DataGridRow.MouseEnter" 
                         Handler="Row_MouseEnter"/>
        </Style>
    </DataGrid.ItemContainerStyle>
</DataGrid>

Row_MouseEnter 事件(在我的 .xaml.cs 文件上)中,我只是在 ViewModel 上“查找命令”并以编程方式触发它。

我真的很想知道是否有办法直接使用 Interaction.Triggers 执行相同的操作

(仅供引用:我正在做的是网格上方有一个面板,它显示鼠标在单击之前所经过的行的详细信息 - 这会触发详细 View )。

最佳答案

是的,您可以通过创建自己的自定义行为类并在 xaml 文件中使用它来直接绑定(bind)到 ViewModel 中的命令。这些链接可能会帮助您入门 - Binding using interactivityBinding through interaction in MVVM

关于wpf - 如何将 WPF DataGridRow 事件与 Interaction.Triggers/MVVM EventToCommand 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11598362/

相关文章:

c# - 当 WPF DataGrid 为空时显示 “No record found” 消息

wpf为什么在绑定(bind)SelectedItem时得到 "NullReferenceException"[编辑]

mvvm-light - PCL 中的 MVVM Light DispatcherHelper

wpf - 多选 WPF 列表框通过单击进行多项选择

xaml - 银光 : Not naming controls in XAML when using MVVM (Silverlight)?

WPF ComboBox 绑定(bind)到非字符串对象

c# - 将默认资源样式应用于所有 DataGridTextColumns

c# - 从列标题中的 TextBox 筛选 WPF DataGrid 值

c# - 您可以/如何在 C# WPF 应用程序中托管完整的 VB6 表单?

c# - WPF INotifyPropertyChanged 用于链接的只读属性