wpf 如何对使用 MultiBinding 转换器的 DataGridTextColumn 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16238289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:41:48  来源:igfitidea点击:

How to Sort a DataGridTextColumn which uses a MultiBinding Converter

wpfsortingdata-bindingdatagrid

提问by Steztric

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;

我使用带有值转换器的多重绑定来提供我的DataContext. 这是 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 SortMemberPathis such that I can sort by the property in my DataContext. However, I get an error on the output window

上的绑定SortMemberPath使得我可以按我的DataContext. 但是,我在输出窗口上收到错误消息

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?

谷歌搜索这个问题会产生一个使用DevExpress 的 DXGrid的结果,但不是使用标准 WPF 数据网格的结果。有谁知道为数据网格列提供排序的正确方法?

回答by Steztric

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.

即不使用绑定,只需直接指定属性名称。