开发者

Pre-sorting a DataGrid in WPF according to localized sort order

I have a WPF4 DataGrid (the one that's included with WPF4) with columns 'Surname', 'First Names' etc. When the user clicks a column header, for example 'Surname', ever开发者_开发技巧ything gets sorted in the right order according to my culture settings, e.g:

Anderson
Rockford
Ångström

I want the 'Surname' column to be pre-sorted, so I've added

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

just like in the answer to the Pre-sorting a DataGrid in WPF question. The problem is, that WPF now uses a different sort order where 'Å' is sorted like 'A' (I guess that's a US sort order):

Andersson
Ångström
Rockford

Manually clicking the 'Surname' header re-sorts the names, this time according to my culture settings.

How do I tell WPF to use the current culture setting when pre-sorting the DataGrid?


Have you tried setting the CollectionViewSource's Culture property? Something like (drycoded) Edited per your comment:

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

I'm not sure why the CollectionViewSource wouldn't use the same culture by default, but if it's not this should be the property that fixes it for you.

See also this article on how binding seems to use the wrong culture when the target is a string, perhaps it can shed some light for you: http://www.nbdtech.com/Blog/archive/2009/02/22/wpf-data-binding-cheat-sheet-update-the-internationalization-fix.aspx

Or this question: WPF XAML Bindings and CurrentCulture Display

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜