WPF XAML绑定和CurrentCulture显示(WPF XAML Bindings and CurrentCulture Display)

编程入门 行业动态 更新时间:2024-10-26 08:26:17
WPF XAML绑定和CurrentCulture显示(WPF XAML Bindings and CurrentCulture Display)

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

<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”)); 当表格开始时,我看到不同的结果。

如果我用默认语言运行表单,一切都很明显。 但是,如果我在代码或启动时更改文化,则绑定到日期和数字值仍会显示en-US格式。 显示的MessageBox.Show()值适当地反映了当前的文化。

问:WPF是否不尊重CurrentCulture的绑定? 如果是的话,究竟是什么决定了用于绑定的文化。 在我的情况下,显然是en-US,但无论我在我的项目中设置为默认语言,它总是在en-US中绑定。

任何想法赞赏...

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

最满意答案

事实证明,WPF在绑定中默认不尊重CurrentCulture,而是默认使用XAML文档中定义的xml:Lang设置,如果未提供,则默认为en-US。 这是相当蹩脚的行为 - 不知道为什么你不会像其他任何UI技术一样应用自动文化格式,但是...

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

// 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);

这篇博文中提供了更多信息:

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

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

更多推荐

本文发布于:2023-08-08 01:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   XAML   WPF   Display   Bindings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!