简体   繁体   English

WPF DataGrid切割列(MVVM)

[英]WPF datagrid cut a column (MVVM)

In WPF datagrid, is is possible to allow the user to cut or delete a column? 在WPF datagrid中,是否可以允许用户剪切或删除列?

If yes, is there any built in support for it? 如果是,是否有内置支持?
If no, how do I do it? 如果没有,我该怎么办? A sample illustration will be very nice. 一个示例图将非常好。

You need two pieces of functionality: 您需要两个功能:

  • one for the user to select/deselect what columns they want to see in the grid 一个供用户选择/取消选择他们想在网格中看到的列
  • one to act upon that info and actually hide/show the affected columns 一个根据该信息进行操作并实际上隐藏/显示受影响的列的控件

Despite the fact you are using MVVM, this is reasonably easy to do, because it can be handled within the View. 尽管您正在使用MVVM,但这样做相当容易,因为它可以在View中处理。 That's right folks, the VM doesn't need to know anything about this - it is purely a view related thing, nothing to do with the data held by the VM. 没错,VM对此一无所知-纯粹是与视图相关的事情,与VM所保存的数据无关。

I am not going to give sample code, you can work that out for yourself. 我不会提供示例代码,您可以自己解决。 Your UI can be as simple as a context menu upon right click in the column, with an option to "Hide this column". 右键单击列,您的UI可以像上下文菜单一样简单,并且可以选择“隐藏此列”。 Of course that is an irreversible one way process, but maybe that is all you want and the column stays hidden until the user next runs the app. 当然,这是一个不可逆的单向过程,但也许这就是您想要的,并且列会一直隐藏,直到用户下次运行该应用程序为止。

Another option is to pop up a modal child window with enough info in it for the user to select which columns they want shown or hidden. 另一个选项是弹出一个模态子窗口,其中包含足够的信息,供用户选择要显示或隐藏的列。 Once again this can be triggered from a context menu item or some other UI action. 再次可以从上下文菜单项或其他UI动作中触发。 At this point you can enumerate the grid columns, create a list of data objects with info on each column (such as name or column heading, and its visibility), create an instance of the appropriate control to show this info (this is the View), assign the data objects to it (ViewModel), then insert the View into the modal child window. 此时,您可以枚举网格列,创建一个数据对象列表,每列上都有信息(例如名称或列标题及其可见性),创建适当控件的实例以显示此信息(这是View ),为其分配数据对象(ViewModel),然后将View插入到模式子窗口中。 Upon closing of the window you can grab that data back, and just iterate through it showing or hiding the columns as per the user's selection. 关闭窗口后,您可以获取该数据,并根据用户的选择遍历显示或隐藏的列。 Remember that this can still happen within the code behind of the original View. 请记住,这仍然可以在原始View背后的代码内发生。 If you are using a region or UI manager like Prism then the child window that i talk of can be managed via that. 如果您使用的是地区或UI管理器(例如Prism),那么可以通过它来管理我所说的子窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM