WPF Datagrid 无需 CTRL 或 Shift 进行多项选择
WPF Datagrid 有两种选择模式:单一或扩展。 WPF ListView 有第三个 - Multiple。此模式允许您单击并选择多行,而无需按住 CTRL 或 Shift 键。有人知道如何为数据网格执行此操作吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我正在创建一个具有类似要求的应用程序,该应用程序适用于触摸屏和桌面。花了一些时间后,我想出的解决方案似乎更干净。
在设计器中,我将以下事件设置器添加到数据网格中:
然后在代码隐藏中,我将事件处理为:
希望它也对其他人有帮助。
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:
Hope it helps somebody else too.
您可以尝试这个简单的解决方法,而无需通过处理预览鼠标按下事件来修改/继承
DataGrid
控件,如下所示:TryFindFromPoint
方法,来自 Philipp Sumi 的博客文章,用于获取DataGridRow
实例您点击的点。通过检查
ModifierKeys
,您仍然可以将 Ctrl 和 Shift 保留为默认行为。此方法的唯一一个缺点是您无法像原来那样通过单击并拖动来执行范围选择。
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.
工具包中的 DataGrid 不支持此功能,看起来像 也将不受支持。该控件尚未准备好用于生产使用的另一个原因。我会选择以下选项之一:
我同意 DataGrid 应该支持这一点,并且我认为您应该 为此提交错误/建议。也许现在将其纳入 .NET 4 还为时不晚..:)
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.. :)
基于上一篇文章,我编写了一个(“类似”)MVVM 代码:
首先将其添加到您的主视图中:
视图的相关部分:
有关 EventToCommandBehavior 的更多信息:
这里
这样,你的ViewModel必须实现这些命令:
最后实现一个方法(模型中的某个位置)查找行。
这个解决方案非常适合我,所以我希望它也对您有帮助。
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.