简体   繁体   中英

WPF datagrid cut a column (MVVM)

In WPF datagrid, is is possible to allow the user to cut or delete a column?

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. 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.

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". 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. 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. 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. If you are using a region or UI manager like Prism then the child window that i talk of can be managed via that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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