WPF DataGrid:获取列绑定属性以进行过滤
我正在尝试为 WPF DataGrid
(来自 WPF Toolkit)开发过滤功能。 我希望用户右键单击任何单元格并从其 CcontextMenu
中选择 Filter
,然后应按单元格的值过滤网格。
我正在尝试 MV-VM 模式。 我的 Windows 数据上下文是 MainWindowViewModel
,它有一个属性 Transactions
。 此属性返回 ObservableCollection
,并且数据网格使用此集合作为其项目源。 因此基本上每一行都绑定到TransactionViewModel(正如您可以猜到的,该网格列出了事务)。 MainWindowsViewModel
有 ICollectionView
用于过滤和跟踪当前选定的行。 DataGrid
的属性 IsSynchronizedWithCurrentItem
设置为“true”,因此 myCollectionView.CurrentItem
为我提供了当前选定的 TransactionViewModel
。
我唯一需要知道的是我需要按哪一列进行过滤。 这取决于用户单击上下文菜单的位置。 因此,我尝试使用上下文菜单项的 CommandProperty 来传递此信息。 我在这里遇到了一个真正的问题。 我尝试了这个:
CommandParameter="{Binding Column.Binding.Path.Path,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type tk:DataGridCell}}}" />
这确实很难看,但这适用于 DataGridTextColumn 。 不幸的是,我也有 DataGridTemplateColumn,但它们不起作用(那里的路径不同,因为我需要到达实际的单元格模板)...
那么我如何实现此功能? 也许整个方法都是错误的? 我没有找到任何有价值的例子。 我唯一发现的是 Codeproject 上的 WPF DataGrid
自动过滤器实现,由于某种原因它根本不起作用......
谢谢。
I am trying to develop a filtering functionality for WPF DataGrid
(from the WPF Toolkit). I want a user to right-click any cell and select Filter
from its CcontextMenu
, and then the grid should be filtered by the cell's value.
I am trying the M-V-VM pattern. My windows's datacontext is MainWindowViewModel
which has a property Transactions
. This property returns ObservableCollection<TransactionViewModel>
, and the data grid uses this collection as its items source. So basically each row is bounded to TransactionViewModel
(as you can guess, this grid lists transactions). MainWindowsViewModel
has ICollectionView
which is used for filtering and tracking the currently selected row. The DataGrid
has its property IsSynchronizedWithCurrentItem
set to "true", so myCollectionView.CurrentItem
gives me the currently selected TransactionViewModel
.
The only thing I still need to know is by which column I need to filter. This depends on where the user clicked the context menu. So I am trying to pass this information using CommandProperty
of the context menu item. And here I have a real problem. I tried this:
CommandParameter="{Binding Column.Binding.Path.Path,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type tk:DataGridCell}}}" />
This is really ugly, but this works for DataGridTextColumn
s. Unfortunately, I have also DataGridTemplateColumn
s, and they don't work (the path is different there, because I need to reach the actual cell template)...
So how can I implement this functionality? Perhaps the whole way is wrong? I didn't find any valuable example on that. The only thing I found is the WPF DataGrid
autofilter implementation on the Codeproject which doesn't work at all for some reason...
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 100% 确定这是否有帮助,但是...
DataGrid 有 CurrentCell,因此您可以在 MainWindowViewModel 中以 TwoWay 模式绑定它。
然后每个“行”都可以指向MainWindowViewModel中定义的DoFilter命令。 这不是一个美观的解决方案(因为视图模型必须知道 DataGrid 单元格类型),但它应该可以工作。
I'm not 100% sure if this would help but...
DataGrid has CurrentCell so you could bind it in TwoWay mode in your MainWindowViewModel.
Then every "row" could point to DoFilter command defined in MainWindowViewModel. It's not a beauty solution (because viewmodel has to know DataGrid Cell type) but it should work.
为什么不直接将单元格作为参数传递,如下所示:
并让命令的 Executed 事件处理查找实际列名称的所有困难部分? 这样您就可以编写所需的所有特殊情况代码。
Why not just pass the cell as a parameter like this:
and let your command's Executed event handle all the hard part of finding the actual column name? That way you can write all the special-case code you need.