【问题标题】:How to use WPF DataGridRow events with Interaction.Triggers / MVVM EventToCommand如何将 WPF DataGridRow 事件与 Interaction.Triggers / MVVM EventToCommand 一起使用
【发布时间】:2012-07-20 20:27:34
【问题描述】:

MVVMLight EventToCommand 可以很容易地用于在您的视图模型上触发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做同样的事情

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

【问题讨论】:

    标签: wpf mvvm-light wpfdatagrid eventtocommand


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 1970-01-01
      • 2020-07-02
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多