如何通过 XAML 绑定设置当前 CultureUI [英] How to set current CultureUI via XAML binding
问题描述
我有一个绑定到字符串的 TextBlock.我希望字符串显示在应用程序的当前 UI 文化中.我想在 XAML 中做到这一点.文本块很简单,如下所示.
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.
推荐答案
您需要设置 FrameworkElement.Language
属性.对整个应用程序执行此操作的最简单方法是覆盖 App
类静态构造函数中的属性元数据:
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)));
}
}
如果您只想为特定控件设置区域性,可以将其 Language
属性绑定到数据上下文的属性:
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}" />
这篇关于如何通过 XAML 绑定设置当前 CultureUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!