WPF Datagrid双击单元格MVVM设计 [英] WPF Datagrid Double Click Cell MVVM Design

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

问题描述

我有一个包含数据网格的WPF应用程序。它绑定到下面显示的List对象 Orders。

I have a WPF application that contains a datagrid. It is bound to my List object "Orders" shown below.

public class OrderBlock
{
  public Settings setting;
  public List<Order> orders;
}
public class Order
{
  public int Amount;
  public string OrderID;
  public string OrderIDDup;
  public string Name;
  public string NameDup;
  public bool DupIDs;
  // and some string, int fields
}

出于某些原因在我的控件中,可能有多个OrderID,因此有OrderIDDup属性。默认情况下,我的数据网格仅显示OrderID,而不显示OrderIDDup。

For reasons out of my control it is possible that there can be more than one OrderID, hence the OrderIDDup property. My datagrid by default shows just the OrderID and not the OrderIDDup.

我要做的是让用户能够单击单元格ID,然后单击另一个加载该窗口以显示其他ID以及两个名称,并让他们选择应使用的ID。

What I would like to do is for the user to be able to click on the cell ID and for another window to load to show them the other ID as well as the two names and let them choose which ID should be used.

我一直在阅读WPF DataGrid的信息。不支持双击单元格的此功能。所以我有点不知所措,因为我应该如何着手解决这个问题。我可以看到的另一个问题是,当我尝试使用MVVM设计时(作为实用的词),如何将这种事件暴露给我的视图模型?

I have been reading that the WPF DataGrid doesn’t support this functionality of double clicking on a cell. So I am a bit lost as how i should start going about this issue. The other issue I can see is that as I am trying (being the operative word) to use a MVVM design how would this kind of event be exposed to my view model?

这也是显示此类信息的最佳方法。

Also is this the best way to go about showing such information.

任何帮助都会很棒,
谢谢,
M

Any help would be great, Thanks, M

推荐答案

可以双击网格,而不是双击单元格

Instead of double-clicking on the cell you may double-click on the grid

<DataGrid.InputBindings>
    <MouseBinding Gesture="LeftDoubleClick" 
    Command="{Binding Edit}" 
    CommandParameter="{Binding ElementName=UsersDataGrid, Path=SelectedItem}" />
</DataGrid.InputBindings>

在ViewModel中

In ViewModel

    public ICommand Edit { get; private set; }

 Edit = new RelayCommand(EditUser, x => _isAdmin);



 private static void EditUser(object usr)
    {
        if (!(usr is User))
            return;

        new UserEditorViewModel(usr as User);
    }

这篇关于WPF Datagrid双击单元格MVVM设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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