wpf DataGrid 绑定命令到行选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15327227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGrid bind command to row select
提问by Bent Rasmussen
I want to execute a command when the user selects a row in a DataGrid.
我想在用户选择 DataGrid 中的一行时执行命令。
I see it is possible to wrap cell contents in buttons (although I don't want the button style) - but I don't want to do it at the cell-level.
我看到可以将单元格内容包装在按钮中(尽管我不想要按钮样式) - 但我不想在单元格级别进行。
I also see it's possible to use behaviours to link a command to an event. But preferably I should not have to resort to behaviors for such a common task.
我还看到可以使用行为将命令链接到事件。但最好我不应该为这样一个常见的任务诉诸行为。
Is it possible to do this via plain old command databinding?
是否可以通过普通的旧命令数据绑定来做到这一点?
So: 1) user clicks DataGrid row 2) command on view model is fired.
所以:1) 用户单击 DataGrid 行 2) 触发视图模型上的命令。
回答by kmatyaszek
You should use "Interactivity" assembly and SelectionChanged
event.
您应该使用“交互性”程序集和SelectionChanged
事件。
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding People}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
Where "i" is namespace:
其中“i”是命名空间:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Also you can write binding to SelectedItem
property of the DataGrid and in the set accessor you can invoke your command, but the first solution that i presented you above is better.
您也可以编写SelectedItem
对 DataGrid 属性的绑定,并在 set 访问器中调用您的命令,但我上面介绍的第一个解决方案更好。
If you want to invoke command from main view model and pass SelectedItem
from DataGrid
you can use CommadParameter
:
如果要从主视图模型调用命令并SelectedItem
从中传递,DataGrid
可以使用CommadParameter
:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding MyCommand}"
CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
When items has own command you can use following code:
当项目有自己的命令时,您可以使用以下代码:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=SelectedItem.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Or if elements has own view model that is assigned to it's DataContext
, you can use following code:
或者,如果元素具有分配给它的自己的视图模型,则DataContext
可以使用以下代码:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding Path=SelectedItem.DataContext.MyCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" />
</i:EventTrigger>
</i:Interaction.Triggers>