开发者

How to set current CultureUI via XAML binding

开发者 https://www.devze.com 2023-01-16 08:20 出处:网络
I have a TextBlock bound to a string. I want the string to be displayed in cu开发者_如何转开发rrent UI culture of the application. I want to do this in XAML. The text block is simple like below.

I have a TextBlock bound to a string. I want the string to be displayed in cu开发者_如何转开发rrent UI culture of the application. I want to do this in XAML. The text block is simple like below.

<TextBlock Text="{Binding Path=Text}"/>


You need to set the FrameworkElement.Language property. The easiest way to do that for the whole application is to override the property metadata in the App class static constructor:

public partial class App : Application
{
    static App()
    {
        FrameworkElement.LanguageProperty.OverrideMetadata(
            typeof(FrameworkElement),
            new FrameworkPropertyMetadata(
                XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
    }
}

If you only want to set the culture for a specific control, you can bind its Language property to a property of your datacontext:

<TextBlock Text="{Binding Something}" Language="{Binding TheLanguage}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消