views:

689

answers:

2

Hello,

I have a UserControl that displays some text of a DP of the UserControl. For this a converter is used. I understand that the culture parameter is the "en-US" culture, unless you specify a different culture in the ConverterCulture value of the binding or the xml:lang attribute.

But how can I change to culture from outside of the UserControl???

This is my 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>

For demonstration purposes the converter simply returns the name of the culture info passed to it:

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();
    }
}

In my window I have two instances of the user control, and each should display a different culture. But both just show the standard "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>
A: 

I'm not an expert at globalisation, in fact I don't think I've ever tried it, but hope this helps.

Here's my UserControl.xaml

<UserControl x:Class="Tab_Question.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="30">
<Grid>
 <TextBlock Text="{Binding Path=CultureName, FallbackValue=bindingError}" />
</Grid>

Heres the code behind UserControl.cs

    public partial class UserControl1 : UserControl
{
 public static DependencyProperty CultureStringProperty =
  DependencyProperty.RegisterAttached("Culture", typeof(String), typeof(UserControl1));

 public static DependencyProperty CultureNameProperty =
  DependencyProperty.Register("CultureName", typeof(String), typeof(UserControl1));

 public String CultureString
 {
  get
  {
   return (String)GetValue(UserControl1.CultureStringProperty);
  }
  set
  {
   if (CultureString != value)
   {
    SetValue(UserControl1.CultureStringProperty, value);
    DoSomethingWithCulture();
   }
  }
 }

 private void DoSomethingWithCulture()
 {
  // Good to continue
  CultureInfo newCulture = CultureInfo.GetCultureInfo(CultureString);
  SetValue(UserControl1.CultureNameProperty, newCulture.Name);
 }

 public UserControl1()
 {
  InitializeComponent();
  this.DataContext = this;
 }
}

And finally heres the Window.xaml

<Window
x:Class="Tab_Question.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Tab_Question"
Title="Window1" Height="300" Width="300">
<StackPanel>
 <local:UserControl1 CultureString="en-us" />
 <local:UserControl1 CultureString="en-gb" />
</StackPanel>

I'll try and explain what I've done, hope it answers the question.

I've setup an attached DependencyProperty on the UserControl that allows you to pass a CultureInfo String to the UserControl when the String is updated the UserControl creates a CultureInfo object that you can store in a member variable, and updates the CultureName DependancyProperty that the TextBlock in the UserControl binds to.

I'm not sure why setting the XmlLang attribute you tried doesn't work but suspect it has something to do with the UI thread only able to have one language.

I know this doesn't match the sample code you provided, but I hope it provides you with a starting ground to modify and adapt.

Ben

TheDuke
Well, this sort of helps, but I would like to use different cultures for the Converter objects used in my UserControl.I could pass the user control itself as the converter parameter and then get the DP from it in the ConvertTo-function. But I would rather not use any converter parameters.
miasbeck
<TextBlock Text="{Binding Path=CultureName, Converter={StaticResource TestConverter}, ConverterCulture={x:Static local:UserControl1.CultureName}}" />If you can figure a way to store the CultureInfo in a static Member variable, then you co9uld use this to supply a different culture to the converter.
TheDuke
@micahtan: Thanks for clearing that up, can't comment on your post yet.
TheDuke
+2  A: 
micahtan
Thanks micahtan! Your solution exactly fits my requirements.Sorry for the late reply. I've been to "nb-NO" for a couple days.
miasbeck