Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Sort a DataGridTextColumn which uses a MultiBinding Converter

I use a multibinding with a value converter to provide the visual display of a collection of items in my DataContext. Here is a snippet of the XAML;

<DataGrid.Columns>
    <DataGridTextColumn x:Name="Column1"
                        SortMemberPath="{Binding Path=SomeDataModelProperty}">
    <DataGridTextColumn.Binding>
        <MultiBinding Converter="{StaticResource MyCustomConverter}">
            <Binding Path="SomeDataModelProperty" />
            <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth" />
            <!-- Other bindings -->
        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

The binding on the SortMemberPath is such that I can sort by the property in my DataContext. However, I get an error on the output window

Cannot find governing FrameworkElement or FrameworkContentElement for target element.

Googling this issue yields a result using DXGrid by DevExpress, but not one using the standard WPF data grid. Does anyone know the correct way to provide the sorting to the data grid column?

like image 915
Steztric Avatar asked Apr 26 '13 13:04

Steztric


1 Answers

I figured it out, thanks to this article. In the end it is quite simple;

<DataGridTextColumn x:Name="Column1"
                    SortMemberPath="SomeDataModelProperty">

i.e. don't use a binding, just specify the property name directly.

like image 156
Steztric Avatar answered Oct 14 '22 00:10

Steztric