MultiBinding上的DataGridColumn SortMemberPath

Bre*_*ent 6 c# wpf xaml binding multibinding

我正在尝试对数字内容进行列排序.多重绑定转换器工作正常.此解决方案将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>
Run Code Online (Sandbox Code Playgroud)

编辑:我找到了一种方法来使数据绑定工作没有多重绑定,但排序仍然无法正常工作.由于DataGrid绑定到一个自定义类,因此我接受了整个值并从中进行转换,从而减少了对MultiBinding的需求.

<DataGridTextColumn x:Name="avgPriceColumn" Binding="{Binding Converter={StaticResource avgPriceConverter}}" SortMemberPath="{Binding Converter={StaticResource avgPriceConverter}}" />
Run Code Online (Sandbox Code Playgroud)

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

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

编辑:

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

Ste*_*der 7

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

想到两个选项:

  1. 在后备对象上提供计算属性(例如"AveragePrice")并绑定到该属性.无需转换器或排序成员路径.

    public double AveragePrice
    {
        get { return TotalDollars / NumberToDivideBy; }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 像这个问题一样指定一个OnSorting事件处理程序.

希望能帮助到你.:)