WPF XAML绑定和CurrentCulture显示 [英] WPF XAML Bindings and CurrentCulture Display

查看:103
本文介绍了WPF XAML绑定和CurrentCulture显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当CurrentCulture更改时,我从XAML文档中看到一些无效的行为.当我在窗口中有类似这样的元素时:

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>

以及MessageBox.Show(NumberValue.ToString("c"));表单开始时,我会看到不同的结果.

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

如果我使用默认语言运行表单,那么一切都很好.但是,如果我更改代码中的区域性或在启动时更改,则日期和数字值的绑定仍显示为美国格式.显示的MessageBox.Show()值适当地反映了当前的区域性.

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.

问题:WPF在绑定中不尊重CurrentCulture吗?如果是这样,究竟是什么确定了用于绑定的区域性.在我看来,这显然是美国语言,但是无论我在项目中设置什么作为默认语言,它始终会在美国语言中绑定.

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

推荐答案

事实证明,WPF在绑定中默认不遵循CurrentCulture,而是默认为XAML文档中定义的xml:Lang设置;如果不提供.这是相当la脚的行为-不确定为什么您不会像其他所有UI技术一样应用自动区域性格式设置,但是...

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

幸运的是,有一个简单的解决方法可以应用于文档的构造函数或Window/UserControl基类:

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

这篇关于WPF XAML绑定和CurrentCulture显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆