gpt4 book ai didi

c# - MultiBinding 上的 DataGridColumn SortMemberPath

转载 作者:太空狗 更新时间:2023-10-29 21:29:03 24 4
gpt4 key购买 nike

我正在尝试对数字内容进行列排序。多重绑定(bind)转换器工作正常。此解决方案会将 SortMemberPath 设置为 null

我尝试了多种方法,并大量搜索了互联网。

出于安全目的,代码已从原始代码修改。

<DataGridTemplateColumn x:Name="avgPriceColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource avgPriceConverter}">
<Binding Path="NumberToDivideBy" />
<Binding Path="TotalDollars" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.SortMemberPath>
<MultiBinding Converter="{StaticResource avgPriceConverter}">
<Binding Path="NumberToDivideBy" />
<Binding Path="TotalDollars" />
</MultiBinding>
</DataGridTemplateColumn.SortMemberPath>
</DataGridTemplateColumn>

编辑:我找到了一种无需多重绑定(bind)即可使数据绑定(bind)工作的方法,但排序仍然不起作用。由于 DataGrid 绑定(bind)到自定义类,我接受了整个值并从中转换,从而减少了对 MultiBinding 的需求。

<DataGridTextColumn x:Name="avgPriceColumn" Binding="{Binding Converter={StaticResource avgPriceConverter}}" SortMemberPath="{Binding Converter={StaticResource avgPriceConverter}}" />

在这两个选项上,SortMemberPath 默认设置为 Binding,因此我不需要像我那样明确定义它

然而,这最终会将 SortMemberPath 值设置为 null,这与适用于我的代码环境的自定义约束冲突,并且不会排序。所以我仍然对更好的解决方案感兴趣。

编辑:

更改了其他地方的冲突代码以允许重复的 SortMemberPath,不支持对某些列进行排序,并且不支持对相邻列值进行排序

最佳答案

SortMemberPath期望属性的名称(例如“TotalDollars”)而不是单个计算的行值。把它想象成标题,你为整列设置一次。您的转换器将返回一个类似于 15 的数字,其中 SortMemberPath 需要一个绑定(bind)路径字符串。

想到的两个选项:

  1. 在您的支持对象上提供一个计算属性(例如“AveragePrice”)并绑定(bind)到它。无需转换器或排序成员路径。

    public double AveragePrice
    {
    get { return TotalDollars / NumberToDivideBy; }
    }
  2. 指定 OnSorting像这样的事件处理程序 question .

希望对您有所帮助。 :)

关于c# - MultiBinding 上的 DataGridColumn SortMemberPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11193203/

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