【问题标题】:DataGridColumn SortMemberPath on MultiBinding多重绑定上的 DataGridColumn SortMemberPath
【发布时间】:2012-06-26 22:29:03
【问题描述】:

我正在尝试对数字内容进行列排序。多绑定转换器工作正常。 此解决方案会将 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>

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

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

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

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

编辑:

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

【问题讨论】:

    标签: c# wpf xaml binding multibinding


    【解决方案1】:

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

    想到的两个选项:

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

      public double AveragePrice
      {
          get { return TotalDollars / NumberToDivideBy; }
      }
      
    2. 指定一个OnSorting 事件处理程序,如question

    希望对您有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 2014-08-26
      • 2019-09-27
      • 2017-02-07
      • 2014-03-31
      • 2011-01-31
      • 2013-07-25
      相关资源
      最近更新 更多