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.
Murven
source share