WPF Datagrid鼠标绑定MVVM [英] WPF Datagrid MouseBinding MVVM

查看:79
本文介绍了WPF Datagrid鼠标绑定MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法通过双击这个数据网格行来在我的视图模型上执行命令:

I managed to handle a double click on one of my datagrid rows to execute a command on my viewmodel by this xaml:

当我单击第一列区域中的某个位置(绑定到只读ID)时,此方法非常理想,但是当在第二列(CustomerNumber,当然是客户编号)中的文本框区域中双击完成时,此方法将失败捕获双击).

This works perfect when I click somewhere in the area of the first column (which is bound to the readonly ID), but fails when the double click is done in the area of the textbox in column two (CustomerNumber, which of course catches the doubleclick).

在两种情况下,哪种方法都可以像MVVM一样处理双击事件?

Which would be a MVVM-like way to handle doubleclicks for both scenarios?

推荐答案

您可以将 DataGridTextColumn 替换为 DataGridTemplateColumn ,并添加 MouseBinding CellEditingTemplate 中的 TextBox :

You could replace the DataGridTextColumn with a DataGridTemplateColumn and add a MouseBinding to the TextBox in the CellEditingTemplate:

<DataGridTemplateColumn Header="Customer Number">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding CustomerNumber}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding CustomerNumber}">
                <TextBox.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" 
                                  Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                </TextBox.InputBindings>
            </TextBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

这篇关于WPF Datagrid鼠标绑定MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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