c# - 如何在双击时将当前选定的 DataGridView 项目发送到 ViewModel 中的命令

标签 c# wpf

是否可以将当前选定的DataGridRow发送到双击InputBinding

我有这样的看法

<DataGrid ItemsSource="{Binding Consignments}" 
            x:Name="dataGridConsignments" 
            Margin="10,47,10,9.6"
            IsReadOnly="True" ColumnWidth="*" AutoGenerateColumns="False"
            xmlns:DataGridDoubleClickCommand="{Binding DataGridDoubleClick}">
    <DataGrid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick"  Command="{Binding RowDoubleClick}"/>
    </DataGrid.InputBindings>

...

viewModel 包含

private ICommand rowDoubleClick;
public ICommand RowDoubleClick
{
    get
    {
        return rowDoubleClick
            ?? (rowDoubleClick= new ActionCommand(() =>
            {
                // I need the selected row here
                MessageBox.Show("asd");
            }));
    }
}

通过此 ActionCommand 实现:

public class ActionCommand : ICommand
{
    private readonly Action _action;

    public ActionCommand(Action action)
    {
        _action = action;
    }

    public void Execute(object parameter)
    {
        _action();
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

最佳答案

您可以尝试将此行添加到 DataGrid

ItemsSource="{Binding Consignments}" SelectedItem="{Binding SelectedConsignment}"

请记住通知“OnPropertyChanged”。

关于c# - 如何在双击时将当前选定的 DataGridView 项目发送到 ViewModel 中的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53396975/

相关文章:

c# - 如何在遇到特定值时更改 DataGrid 行颜色?

wpf - WPF MVVM Prism 中基于ASP.net的基于表单的身份验证

wpf - 根据网格的列宽更改矩形填充

c# - 索引时出现错误的请求错误

c# - 资源部分中的 WPF DataContextProxy

c# - 将 View 模型列表传递给 mvc Controller 操作

c# - 基于处理器类型在 C# 应用程序中运行可变线程

c# - 将 child 添加到 UserControl

c# - 如何在转换器中将对象转换为 int?

C# MongoDB反序列化id属性