How to set current CultureUI via XAML binding

对着背影说爱祢 提交于 2019-12-12 19:56:46

问题


I have a TextBlock bound to a string. I want the string to be displayed in current UI culture of the application. I want to do this in XAML. The text block is simple like below.

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


回答1:


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}" />


来源:https://stackoverflow.com/questions/3735064/how-to-set-current-cultureui-via-xaml-binding

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