DataGridColumn SortMemberPath on MultiBinding

纵饮孤独 提交于 2019-12-23 09:27:20

问题


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 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>

EDIT: 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}}" />

On both these options SortMemberPath is default set to Binding so I don't need to explicitly define it as I have

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.

EDIT:

Changed conflicting code elsewhere to allow duplicate SortMemberPath's, don't support sorting on some columns, and for some sort off neighbouring-column value


回答1:


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:

  1. 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; }
    }
    
  2. Specify an OnSorting event handler like in this question.

Hope it helps. :)



来源:https://stackoverflow.com/questions/11193203/datagridcolumn-sortmemberpath-on-multibinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!