DataGridColumn SortMemberPath on MultiBinding
Asked Answered
M

1

7

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

Mathers answered 25/6, 2012 at 16:22 Comment(0)
T
8

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. :)

Telegraphone answered 22/3, 2013 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.