The WPF Datagrid has two selection modes, Single or Extended. The WPF ListView has a third - Multiple. This mode allows you to click and select multiple rows without CTRL or Shift being held down. Anyone know how to do this for the datagrid?
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
Based on a previous article, i wrote a ("like") MVVM code:
Firstly add this to your main View:
The relevant part of View:
More information about EventToCommandBehavior: here
In this way, your ViewModel must implement these commands:
Finally implement a method (somewhere in Model) to find the row(s).
This solution works for me perfectly so i hope it will help for you too.
This is not supported in the DataGrid in the toolkit, and it looks like it won't be supported when the DataGrid is shipped with .NET 4 either. Yet another reason why this control is not ready for production use. I would go with one of these options:
I agree that the DataGrid should support this and I think you should file a bug/suggestion for this anyway. Maybe it's not too late to get it into .NET 4.. :)
You can try this simple workaround without having to modifying/inheriting
DataGrid
control by handling preview mouse down event as follows:The
TryFindFromPoint
method, from a blog post by Philipp Sumi, is used to get theDataGridRow
instance from point you clicked.By checking
ModifierKeys
, you can still keep Ctrl and Shift as default behavior.Only one draw back from this method is that you can't click and drag to perform range select like it can originally.
I was creating an application with a similar requirement that would work for both touchscreen and desktop. After spending some time on it, the solution I came up with seems cleaner. In the designer, I added the following event setters to the datagrid:
Then in the codebehind, I handled the events as:
Here is the code for the helper method GetVisualParentByType:
Hope it helps somebody else too.