WPF XML:LANG /语言绑定 [英] WPF xml:lang/Language binding
问题描述
如何能绑定列表框的或texblock的语言属性(或者xml:lang属性)。
how can i bind Listbox's or texblock's Language attribute (or xml:lang attribute).
我要显示在特定的语言环境的月份名称
i want to show month names in the specific language setting
例如:
<TextBlock x:Name="Date" xml:lang="{Binding Lang}">
<TextBlock.Text>
<MultiBinding StringFormat=" {0:dd.MMM.yyyy}-{1:dd.MMM.yyyy}">
<Binding Path="Date1"/>
<Binding Path="Date2"/>
</MultiBinding>
</TextBlock.Text>
结果应根据郎财产是:
result should be according to Lang property:
01.Apr.2011 - 01.Apr.2011的en-US
01.Apr.2011 - 01.Apr.2011 en-US
或01.Nis.2011 - 02.Nis.2011 TR-TR
or 01.Nis.2011 - 02.Nis.2011 tr-TR
或....
它给XamlParseException:语言属性不能转换为System.Windows.Markup.XmlLanguage类型
it gives XamlParseException : Language attribute cannot convert to System.Windows.Markup.XmlLanguage type (that is not exact Error Text. )
任何想法?
推荐答案
在应用程序的启动
事件,添加此指令:
In the Startup
event of the application, add this instruction:
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
这将覆盖语言
属性的默认值与当前的文化,为整个应用程序。
It will override the default value of the Language
property to the current culture, for the whole application.
编辑:好吧,我误解你的问题...
ok, I had misunderstood your question...
如果你想在语言
属性绑定到包含字符串 IetfLanguageTag
,你需要一个转换器:
If you want to bind the Language
property to a string containing the IetfLanguageTag
, you need a converter:
[ValueConversion(typeof(string), typeof(XmlLanguage))]
public class IetfTagToXmlLanguageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string tag = value as string;
if (tag == null)
return Binding.DoNothing;
return XmlLanguage.GetLanguage(tag);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
XmlLanguage lang = value as XmlLanguage;
if (lang == null)
return Binding.DoNothing;
return lang.IetfLanguageTag;
}
}
声明转换器中的XAML资源:
Declare the converter in the XAML resources:
<local:IetfTagToXmlLanguageConverter x:Key="languageConverter" />
和在绑定使用转换器:
<TextBlock Language="{Binding Lang, Converter={StaticResource languageConverter}}">
这篇关于WPF XML:LANG /语言绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!