我有一個UserControl顯示UserControl的DP的一些文本。爲此使用了一個轉換器。我知道文化參數是「en-US」文化,除非您在綁定的ConverterCulture值或xml:lang屬性中指定不同的文化。如何更改Converter的CultureInfo?
但是,我怎樣才能改變從用戶的外部文化?
這是我的用戶:
<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();
}
}
在我的窗口,我有用戶的兩個實例控制,並且每個人都應該展示不同的文化。但都顯示標準的「美國」。
<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>
好吧,這種幫助,但我想爲我的UserControl中使用的Converter對象使用不同的文化。 我可以將用戶控件本身作爲轉換器參數,然後從ConvertTo函數中獲取DP。但我寧願不使用任何轉換器參數。 – miasbeck 2009-05-25 13:55:39