一个DataGridColumn SortMemberPath上MultiBinding [英] DataGridColumn SortMemberPath on MultiBinding
问题描述
我想对数字内容的列排序。多结合转换器工作正常。
该解决方案将设置SortMemberPath为空
I'm trying to have column sort on numeric content. Multi-binding converter works fine. This solution will set SortMemberPath to null
我已经尝试了多种方式,大幅度冲刷互联网。
I've tried a variety of ways, and scoured the internet substantially.
code已经从原来的修改为安全起见。
Code has been modified from original for security purposes.
<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>
编辑:
我找到一种方式来获得数据绑定不multibinding工作,但排序仍然无法正常工作。由于DataGrid绑定到一个自定义类,我采取整体价值,并从该转换,从而减少了MultiBinding的需求。
I found a way to get data binding to work without multibinding, but sorting still doesn't work. Since DataGrid is bound to a custom class, I take in whole value and convert from that, thus reducing the need for MultiBinding.
<DataGridTextColumn x:Name="avgPriceColumn" Binding="{Binding Converter={StaticResource avgPriceConverter}}" SortMemberPath="{Binding Converter={StaticResource avgPriceConverter}}" />
在这两个选项SortMemberPath是默认设置为绑定,所以我并不需要明确地定义它,因为我有
On both these options SortMemberPath is default set to Binding so I don't need to explicitly define it as I have
不过,这最终设置SortMemberPath值设置为null它适用于我的code环境自定义约束,矛盾,不排序。所以,我仍然有兴趣更好的解决方案。
However this ends up setting SortMemberPath value to null which conflicts with custom constraints applicable to my code environment, and doesn't sort. So I am still interested in better solutions.
编辑:
变更冲突的code别处允许重复SortMemberPath的,不支持对某些列排序,以及一些邻国断柱值
Changed conflicting code elsewhere to allow duplicate SortMemberPath's, don't support sorting on some columns, and for some sort off neighbouring-column value
推荐答案
<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcolumn.sortmemberpath%28v=vs.110%29.aspx\">SortMemberPath期待一个属性的名称(例如,TotalDollars),而不是个人计算行值。认为它像头,你整个列设置一次。你的转换器会返回一个数字,如15,其中SortMemberPath想要一个绑定路径字符串。
SortMemberPath is expecting the name of a property (e.g. "TotalDollars") not an individual computed row value. Think of it like the header, you set it once for the whole column. Your converter would be returning a number like 15 where SortMemberPath wants a binding path string.
这浮现在脑海中有两个选项:
Two options that come to mind:
-
提供您的支持对象(例如AveragePrice),并绑定到一个计算的属性。没有必要转换器或排序成员路径。
Provide a computed property on your backing object (e.g. "AveragePrice") and bind to that. No converter or sort member path necessary.
public double AveragePrice
{
get { return TotalDollars / NumberToDivideBy; }
}
指定的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.onsorting.aspx\">OnSorting事件处理像这样的question.
希望它帮助。 :)
这篇关于一个DataGridColumn SortMemberPath上MultiBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!