WPF XAML StringFormat: Workaround in C # 4.0?

Work around ...

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

... is still used (mentioned here: StringFormat Problems with localization in wpf ).

Instead, while I ported my application from 3.5SP1 to 4.0, it worked. But now, in 4.0, it did not work again. Does anyone experience this?

EDIT: Now it doesn't even work in version 3.5SP1. I think this has something to do with installing 4.0, as it worked previously.

It does not work by adding a workaround or removing it. I even tried to add ...

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

to Window . This also did not work.

+4
source share
1 answer

1. Make sure that you override the default LanguageProperty value as early as possible. A static application builder is the best choice. This is important because BindingExpression caches the value of this property and does not overestimate it for performance reasons.

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

3. Overriding the metadata of the Language properties does not work if you specify the xml:lang attribute somewhere on top of the tree. For instance. if you say:

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

You will receive Italian currency no matter what you set in the property metadata.

4. Overriding the metadata of the Language properties is not affected if you specify the ConverterCulture property in the binding. For instance. if you say: <TextBlock Text="{Binding StringFormat=C, ConverterCulture=ja}"/> you will get Japanese currency regardless of what you set either in the property metadata or in the xml:lang attribute.

This behavior has not changed between frameworks, as far as I know.

Hope this helps

0
source

Source: https://habr.com/ru/post/1307646/


All Articles