Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

localization - WPF XAML Bindings and CurrentCulture Display

I'm seeing some invalid behavior from XAML documents when the CurrentCulture is changed. When I have some elements like this in a Window:

<Window x:Class="WpfLocalizationLocBaml.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"
        x:Name="wndTest"
    Title="Test" Height="300" Width="300">
    <StackPanel>

        <TextBlock x:Name="lblCultureName" 
                   Text="{Binding Source={x:Static glob:CultureInfo.CurrentCulture},
                                  Path=DisplayName}" />


        <TextBlock x:Name="lblLocaleDateValue" 
                   Text="{Binding ElementName=wndTest, Path=TestDate}"/>

        <TextBlock x:Name="lblLocaleNumberValue" 
                   Text="{Binding ElementName=wndTest,Path=NumberValue,StringFormat=c}" />

    </StackPanel>
</Window>

as well as a MessageBox.Show( NumberValue.ToString("c") ); when the form starts I'm seeing different results.

If I run the form with the default language all is well obviously. However, if I change the culture in code or at startup the bindings to the date and number values still show en-US formatting. The MessageBox.Show() value displayed appropriately reflects the current culture.

Question: Does WPF not respect CurrentCulture in bindings? And if so what exactly determines the culture that is used for the bindings. It's clearly en-US in my case, but regardless what I set in my project as the default language it always binds in en-US.

Any ideas appreciated...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It turns out that WPF does not respect the CurrentCulture by default in bindings, and instead defaults to xml:Lang setting defined in the XAML document or en-US if not provided. This is rather lame behavior - not sure why you would NOT have automatic culture formatting applied as every other UI technology, but...

Luckily there's an easy workaround that can be applied in the document's constructor or a Window/UserControl base class:

        // MAKE SURE you set the language of the page explicitly or else
        // all number and date formatting occurs using 
        this.Language = XmlLanguage.GetLanguage(
                        CultureInfo.CurrentCulture.IetfLanguageTag);

There's more information available in this blog post:

http://www.west-wind.com/weblog/posts/796725.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...