【发布时间】:2009-05-29 16:58:07
【问题描述】:
我有一个显示用户控件 DP 的一些文本的用户控件。为此,使用了转换器。我知道文化参数是“en-US”文化,除非您在绑定的 ConverterCulture 值或 xml:lang 属性中指定不同的文化。
但是我怎样才能从 UserControl 之外更改为文化???
这是我的用户控件:
<UserControl x:Class="CultInfoConverter.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:CultInfoConverter"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Tag="{x:Null}">
<UserControl.Resources>
<my:MyConverter x:Key="MyConverter" />
</UserControl.Resources>
<StackPanel Orientation="Horizontal" >
<TextBlock Margin="8">My converter culture is</TextBlock>
<TextBlock FontWeight="Bold" Text="{Binding Tag, Converter={StaticResource MyConverter}}" />
</StackPanel>
</UserControl>
出于演示目的,转换器仅返回传递给它的文化信息的名称:
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return culture.Name;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在我的窗口中,我有两个用户控件实例,每个实例都应显示不同的文化。但两者都只显示标准的“en-US”。
<Window x:Class="CultInfoConverter.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:my="clr-namespace:CultInfoConverter">
<StackPanel>
<TextBlock Margin="8">using xml:lang="de-DE"</TextBlock>
<my:MyUserControl xml:lang="de-DE"/>
<TextBlock Margin="8">using xml:lang="fr-FR"</TextBlock>
<my:MyUserControl xml:lang="fr-FR"/>
</StackPanel>
</Window>
【问题讨论】:
标签: .net wpf localization