【问题标题】:Pre-sorting a DataGrid in WPF according to localized sort order根据本地化排序顺序在 WPF 中对 DataGrid 进行预排序
【发布时间】:2011-05-22 12:07:15
【问题描述】:

我有一个 WPF4 DataGrid(包含在 WPF4 中),其中包含“姓氏”、“名字”等列。当用户单击列标题时,例如“姓氏”,所有内容都会在右侧排序根据我的文化设置排序,例如:

Anderson
Rockford
Ångström

我希望对“姓氏”列进行预排序,所以我添加了

    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="SurName" />
    </CollectionViewSource.SortDescriptions>

就像Pre-sorting a DataGrid in WPF 问题的答案一样。问题是,WPF 现在使用不同的排序顺序,其中“Å”的排序类似于“A”(我猜这是美国的排序顺序):

Andersson
Ångström
Rockford

手动单击“姓氏”标题会重新排序名称,这一次是根据我的文化设置。

如何告诉 WPF 在对 DataGrid 进行预排序时使用当前区域性设置?

【问题讨论】:

    标签: wpf wpfdatagrid


    【解决方案1】:

    您是否尝试过设置 CollectionViewSource 的 Culture 属性?类似(干编码)根据您的评论编辑

    ... in your namespaces
    xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"
    ....
    
    
    <CollectionViewSource Culture="{x:Static glob:CultureInfo.CurrentCulture}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="SurName" />
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
    

    我不确定为什么 CollectionViewSource 默认不使用相同的文化,但如果不是,这应该是为您修复它的属性。

    另请参阅这篇文章,了解当目标是字符串时绑定似乎如何使用错误的文化,也许它可以为您提供一些启示:http://www.nbdtech.com/Blog/archive/2009/02/22/wpf-data-binding-cheat-sheet-update-the-internationalization-fix.aspx

    或者这个问题:WPF XAML Bindings and CurrentCulture Display

    【讨论】:

    • Culture="{x:Static glob:CultureInfo.CurrentCulture}" 有效(而不是 ...CurrentUICulture 出于某种原因),谢谢!不太确定我为什么需要它,因为我还使用了您的备忘单链接中描述的OverrideMetadata 修复。
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2010-12-24
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多