Question

I have a datagrid with a textcolum using a MultiValueConverter. The converter got 2 values. The first depend of the current Item and the second of a TextBlock. The value displayed is what I want.

<TextBox x:Name="phases"></TextBox>
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeRows="False"
         ItemsSource="{Binding MySource}" RowDetailsVisibilityMode="Collapsed" RowHeaderWidth="0" 
          SelectionMode="Single">
    <DataGrid.Columns>
        <DataGridTextColumn Width="Auto" Header="Pos">
            <DataGridTextColumn.Binding>
                <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                    <Binding ElementName="phases" Path="Text" />
                    <Binding />
                </MultiBinding>
            </DataGridTextColumn.Binding>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid >

The Value display in the column is what I want. The problem is that I can't sort by this colum. I try to add something like that:

<DataGridTextColumn.SortMemberPath>
    <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
        <Binding ElementName="phases" Path="Text" />
        <Binding />
    </MultiBinding>
</DataGridTextColumn.SortMemberPath>

But I get an "Cannot find governing FrameworkElement or FrameworkContentElement for target element." error. I change to:

<DataGridTextColumn.SortMemberPath>
    <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
        <Binding Path="Text" Source="{x:Reference phases}" />
        <Binding Path="" />
    </MultiBinding>
</DataGridTextColumn.SortMemberPath>

Then the first line is ok, but for the second, I can't get the currentItem.

I try to used SortEvent, but I can only add SortDescription without any logic ( I have it in my converter).

Is any way to sort a column when using a multiValueConverter?

Was it helpful?

Solution

I think this is the same question as here: DataGridColumn SortMemberPath on MultiBinding

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top