DataGrid 绑定命令到行选择 [英] DataGrid bind command to row select

查看:20
本文介绍了DataGrid 绑定命令到行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户选择 DataGrid 中的一行时执行命令.

I want to execute a command when the user selects a row in a 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?

因此:1) 用户单击 DataGrid 行 2) 触发了视图模型上的命令.

So: 1) user clicks DataGrid row 2) command on view model is fired.

推荐答案

你应该使用Interactivity"程序集和 SelectionChanged 事件.

You should use "Interactivity" assembly and SelectionChanged event.

<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>

其中i"是命名空间:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

您也可以编写绑定到 DataGrid 的 SelectedItem 属性,并在 set 访问器中调用您的命令,但我上面介绍的第一个解决方案更好.

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.

如果你想从主视图模型调用命令并从 DataGrid 传递 SelectedItem 你可以使用 CommadParameter:

If you want to invoke command from main view model and pass SelectedItem from DataGrid you can use 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>

或者如果元素有自己的视图模型并分配给它的 DataContext,你可以使用以下代码:

Or if elements has own view model that is assigned to it's DataContext, you can use following code:

 <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>

这篇关于DataGrid 绑定命令到行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆