简体   繁体   English

WPF DataGrid:获取列绑定属性以进行过滤

[英]WPF DataGrid: Get column binding property for filtering

I am trying to develop a filtering functionality for WPF DataGrid (from the WPF Toolkit). 我正在尝试为WPF DataGrid开发筛选功能(来自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. 我希望用户右键单击任何单元格,然后从其CcontextMenu选择“ Filter ,然后应使用该单元格的值来过Filter格。

I am trying the MV-VM pattern. 我正在尝试MV-VM模式。 My windows's datacontext is MainWindowViewModel which has a property Transactions . 我的Windows的datacontext是MainWindowViewModel ,它具有一个Transactions属性。 This property returns ObservableCollection<TransactionViewModel> , and the data grid uses this collection as its items source. 此属性返回ObservableCollection<TransactionViewModel> ,并且数据网格使用此集合作为其项源。 So basically each row is bounded to TransactionViewModel (as you can guess, this grid lists transactions). 因此,基本上每一行都绑定到TransactionViewModel (您可以猜到,此网格列出了事务)。 MainWindowsViewModel has ICollectionView which is used for filtering and tracking the currently selected row. MainWindowsViewModel具有ICollectionView ,该ICollectionView用于筛选和跟踪当前选定的行。 The DataGrid has its property IsSynchronizedWithCurrentItem set to "true", so myCollectionView.CurrentItem gives me the currently selected TransactionViewModel . DataGrid的属性IsSynchronizedWithCurrentItem设置为“ true”,因此myCollectionView.CurrentItem给我当前选择的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. 因此,我尝试使用上下文菜单项的CommandProperty传递此信息。 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. 这确实很丑陋,但这适用于DataGridTextColumn 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)... 不幸的是,我也有DataGridTemplateColumn ,它们不起作用(路径不同,因为我需要到达实际的单元格模板)...

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... 我发现的唯一一件事是Codeproject上的WPF DataGrid自动筛选器实现由于某种原因根本无法使用...

Thank you. 谢谢。

I'm not 100% sure if this would help but... 我不确定100%是否会有所帮助,但是...

DataGrid has CurrentCell so you could bind it in TwoWay mode in your MainWindowViewModel. DataGrid具有CurrentCell,因此您可以在MainWindowViewModel中以TwoWay模式绑定它。 Then every "row" could point to DoFilter command defined in MainWindowViewModel. 然后,每个“行”都可以指向MainWindowViewModel中定义的DoFilter命令。 It's not a beauty solution (because viewmodel has to know DataGrid Cell type) but it should work. 这不是一个完美的解决方案(因为viewmodel必须知道DataGrid Cell类型),但它应该可以工作。

Why not just pass the cell as a parameter like this: 为什么不将单元格作为这样的参数传递:

CommandParameter=
  "{Binding RelativeSource={RelativeSource FindAncestor,tk:DataGridCell,1}}" />

and let your command's Executed event handle all the hard part of finding the actual column name? 并让您命令的Executed事件处理查找实际列名的所有困难部分? That way you can write all the special-case code you need. 这样,您可以编写所需的所有特殊情况代码。

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

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