gpt4 book ai didi

WPF 数据网格 : Get column binding property for filtering

转载 作者:行者123 更新时间:2023-12-03 10:20:03 25 4
gpt4 key购买 nike

我正在尝试为 WPF DataGrid 开发过滤功能(来自 WPF 工具包)。我希望用户右键单击任何单元格并选择 Filter来自其CcontextMenu ,然后网格应按单元格的值进行过滤。

我正在尝试 M-V-VM 模式。我的 windows 的数据上下文是 MainWindowViewModel其中有一个属性 Transactions .此属性返回 ObservableCollection<TransactionViewModel> ,并且数据网格将此集合用作其项目源。所以基本上每一行都是有界的 TransactionViewModel (你可以猜到,这个网格列出了交易)。 MainWindowsViewModelICollectionView用于过滤和跟踪当前选定的行。 DataGrid有它的属性(property)IsSynchronizedWithCurrentItem设置为“真”,所以 myCollectionView.CurrentItem给我当前选择的TransactionViewModel .

我仍然需要知道的唯一一件事是我需要过滤哪一列。这取决于用户单击上下文菜单的位置。所以我尝试使用 CommandProperty 传递这些信息的上下文菜单项。在这里我有一个真正的问题。我试过这个:

CommandParameter="{Binding Column.Binding.Path.Path, 
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type tk:DataGridCell}}}" />

这真的很难看,但这适用于 DataGridTextColumn s。不幸的是,我也有 DataGridTemplateColumn s,并且它们不起作用(那里的路径不同,因为我需要到达实际的单元格模板)...

那么我该如何实现这个功能呢?也许整个方式都是错误的?我没有找到任何有值(value)的例子。我唯一找到的是 WPF DataGrid Codeproject 上的自动过滤器实现由于某种原因根本不起作用......

谢谢你。

最佳答案

我不是 100% 确定这是否会有所帮助,但是......

DataGrid 具有 CurrentCell,因此您可以在 MainWindowViewModel 中以 TwoWay 模式将其绑定(bind)。
然后每个“行”都可以指向 MainWindowViewModel 中定义的 DoFilter 命令。这不是一个漂亮的解决方案(因为 viewmodel 必须知道 DataGrid Cell 类型),但它应该可以工作。

关于WPF 数据网格 : Get column binding property for filtering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1171357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com