使用"真正的" CultureInfo.CurrentCulture在WPF绑定,不从的CultureInfo IetfLanguageTag [英] Use "real" CultureInfo.CurrentCulture in WPF Binding, not CultureInfo from IetfLanguageTag
问题描述
在我的情况:
我有一个TextBlock绑定到DateTime类型的属性。
我希望它显示为用户的区域设置如是说。
< TextBlock的文本={结合日期,的StringFormat = {} {0:D}}/>
我设置语言属性作为 WPF XAML绑定和显示的CurrentCulture
说:
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
但随着这条线code的它只是显示文本的CultureInfo的默认格式
用的CurrentCulture的IetfLanguageTag说,还不如在系统区域设置中选择的有效值说:
(例如,为De-DE的 DD.MM.YYYY 使用而不是选择 YYYY-MM-DD )
有没有一种方法绑定使用正确的格式不会对定义ConverterCulture每一个绑定?
在code
的String.Format({0:D},日期);
使用正确的文化设置。
编辑:
另一种方式为希望不工作(如this.Language = ...一样):
的xmlns:水珠=CLR的命名空间:System.Globalization;装配= mscorlib程序
和
<绑定源={X:静态水珠:CultureInfo.CurrentCulture}
PATH =IetfLanguageTag
ConverterCulture ={X:静态水珠:CultureInfo.InvariantCulture}/>
您可以创建结合(例如CultureAwareBinding)创建时的自动设置ConverterCulture为当前文化的子类。
这不是一个完美的解决方案,但它可能是唯一的一个,因为追溯强制绑定尊重文化可以打破WPF等code依赖于这种行为。
让我知道如果你需要更多的帮助!
In my case:
I have a TextBlock Binding to a property of type DateTime. I want it to be displayed as the Regional settings of the User says.
<TextBlock Text="{Binding Date, StringFormat={}{0:d}}" />
I am setting Language property as WPF XAML Bindings and CurrentCulture Display says:
this.Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag);
But with this line of code it just displays the text as the default format of CultureInfo with IetfLanguageTag of CurrentCulture says, not as the effective value selected in systems region settings says:
(e.g. for "de-DE" dd.MM.yyyy is used instead of selected yyyy-MM-dd)
Is there a way Binding uses the correct format without defining ConverterCulture on every single Binding?
In code
string.Format("{0:d}",Date);
uses the right Culture settings.
edit:
another way which doesn't work as desired (like this.Language = ... does):
xmlns:glob="clr-namespace:System.Globalization;assembly=mscorlib"
and
<Binding Source="{x:Static glob:CultureInfo.CurrentCulture}"
Path="IetfLanguageTag"
ConverterCulture="{x:Static glob:CultureInfo.InvariantCulture}" />
You can create a subclass of binding (e.g. CultureAwareBinding) which sets the ConverterCulture automatically to the current culture when created.
It's not a perfect solution, but it's probably the only one, since retroactively forcing Binding to respect the culture could break other code in WPF which depends on this behavior.
Let me know if you need more help!
这篇关于使用&QUOT;真正的&QUOT; CultureInfo.CurrentCulture在WPF绑定,不从的CultureInfo IetfLanguageTag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!