WPF XAML StringFormat: Culture Workaround broken in C# 4.0?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 17:53:08

问题


The work around...

FrameworkElement.LanguageProperty.OverrideMetadata(
 typeof(FrameworkElement), 
 new FrameworkPropertyMetadata(
  XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

...used to work till now (mentioned here: StringFormat Localization issues in wpf).

Instead till I ported my application from 3.5SP1 to 4.0, it was working. But now in 4.0 it stopped working again. Anybody experiencing this?

EDIT: It is now not even working in 3.5SP1. I think this has something to do with the installation of 4.0 as previously this was working.

It is not working by either adding the workaround or removing it. I even tried adding...

CultureInfo.CurrentCulture.ClearCachedData();
this.Language = XmlLanguage.GetLanguage( CultureInfo.CurrentCulture.IetfLanguageTag);

to Window constructor. This also didn't worked.


回答1:


1. Make sure you are overriding default value of LanguageProperty as early as possible. App's static constructor is the best bet. This is important because BindingExpression caches value of this property and does not reevaluate it afterwards for performance reasons.

2. What is your CultureInfo.CurrentCulture? Are you sure it's the one you expect to see?

3. Overriding Language property metadata has no effect if you you specify xml:lang attribute somewhere upper in the tree. E.g. if you say:

<StackPanel xml:lang="it">
  <TextBlock Text="{Binding StringFormat=C}"/>
</StackPanel>

You'll get Italian currency no matter what you set in property metadata.

4. Overriding Language property metadata has no effect if you specify ConverterCulture property in binding. E.g. if you say: <TextBlock Text="{Binding StringFormat=C, ConverterCulture=ja}"/> you'll get Japanese currency no matter what you set either in property metadata or in xml:lang attribute.

This behavior was not changed between frameworks as far as I know.

Hope this helps



来源:https://stackoverflow.com/questions/2689771/wpf-xaml-stringformat-culture-workaround-broken-in-c-sharp-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!