How to Toggle UI Data Culture on-the-fly in Silverlight

I have a TextBlock control that is bound to a DateTime attribute.

The text is displayed like this:

Thursday October 21, 2010

I need to switch UI Culture on the fly using something like this:

Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-de"); 

I tried this to force bind to recalc:

 var bindingExpression = textBlock.GetBindingExpression(TextBlock.TextProperty); bindingExpression.UpdateSource(); 

But I still see Thursday instead of Donnerstag ...

How do I proceed? Any ideas?

+3
source share
2 answers

I found a better approach that only requires updating the root image.

 public sealed class Localizer : INotifyPropertyChanged { public Localizer() { Culture = Thread.CurrentThread.CurrentCulture; } XmlLanguage _language; public XmlLanguage Language { get { return _language; } private set { _language = value; RaiseOnPropertyChanged("Language"); } } CultureInfo _culture; public CultureInfo Culture { get { return _culture; } set { Contract.Requires(value != null); if (_culture == value) return; _culture = value; Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = value; Language = XmlLanguage.GetLanguage(value.Name); RaiseOnPropertyChanged("Culture"); } } protected void RaiseOnPropertyChanged(string propName) { var e = OnPropertyChanged; if (e != null) e(this, new PropertyChangedEventArgs(propName)); } public event PropertyChangedEventHandler OnPropertyChanged; } 

Now add this instance to the application resources:

 <nt:Localizer x:Key="Localizer"/> 

Now bind it to the root visual (fe Frame, UserControl or Page) as follows:

 <UserControl ... Language="{Binding Language, Source={StaticResource Localizer}}"> 
+8
source

You can use the following converter:

 public class StringFormatter : IValueConverter { public String Format { get; set; } public String Culture { get; set; } #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (!String.IsNullOrEmpty(Culture)) { culture = new System.Globalization.CultureInfo(Culture); } else { culture = System.Threading.Thread.CurrentThread.CurrentUICulture; } if (value == null) { return value; } if (String.IsNullOrEmpty(Format)) { return value; } return String.Format(culture, Format, value).Trim(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion } 

Then you can set CurrentUICulture and force change the binding, and the converter will use the new culture.

If you want to display the date in long format, declare the converter in XAML as follows:

 <local:StringFormatter x:Key="LongDateFormatter" Format=" {0:D}" /> 

And then use it in your text block as follows:

 <TextBlock x:Name="DateText" Text="{Binding DateTime.Date, Converter={StaticResource LongDateFormatter}, Mode=OneWay}"/> 

In code, you can do something like this to make the binding change:

 Thread.CurrentThread.CurrentUICulture = new CultureInfo(desiredCultureString); var tempDateTime = this.DateTime; this.DateTime = default(DateTime); this.DateTime = tempDateTime; 

Of course, there are other ways to force change, and you probably need to change other fields to a new culture as well, but this is a general idea on how to handle this.

0
source

All Articles