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

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

问题描述

我想在用户在DataGrid中选择一行时执行一个命令。

I want to execute a command when the user selects a row in a DataGrid.

我看到可以将单元格内容包裹在按钮中t想要的按钮样式) - 但我不想在单元格级别。

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.

如果你想从主视图模型中调用命令并传递 SelectedItem DataGrid 可以使用 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天全站免登陆