【问题标题】:Bind same command to datagrid double click将相同的命令绑定到数据网格双击
【发布时间】:2018-02-01 18:31:02
【问题描述】:

我有一个配置为使用绑定执行命令的菜单项:

<MenuItem Margin="2" 
     Header="Process something"
     Command="{Binding SomethingCommand}" 
     IsEnabled="{Binding SomethingIsEnabled}">
</MenuItem>

它还配置是否启用此项。

现在,当用户双击数据网格中的一行并且只有“SomethingisEnabled”为真时,我需要执行相同的命令。

如何配置数据网格来做到这一点?

【问题讨论】:

  • 为什么有一个单独的SomethingIsEnabled 属性?为什么不让SomethingCommand.CanExecute 判断命令是否可用呢?如果CanExecute 返回false,MenuItem 将被自动禁用。
  • 我不知道。我会试一试。但是,数据网格呢?

标签: c# wpf


【解决方案1】:

在 xaml 中设置 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<DataGrid x:Name="dataGrid" ItemsSource="{Binding DataList}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <i:InvokeCommandAction Command="{Binding SomethingCommand}" CommandParameter="{Binding ElementName=dataGrid, Path=SelectedItem}"></i:InvokeCommandAction>
                </i:EventTrigger>
            </i:Interaction.Triggers>
</DataGrid>

这将调用中继命令 SomethingCommand 并在双击的网格上传递所选项目(如果有,否则为 null)。

【讨论】:

  • System.Windows.Interactivity,我使用 mvvm light (galasoft),它可能已将其自动添加到我的项目中。您可以通过 nuget 获取。
猜你喜欢
  • 2011-01-07
  • 2010-10-09
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
  • 2016-03-01
  • 1970-01-01
相关资源
最近更新 更多