您可以使用以下转换器:
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
}
然后您可以设置 CurrentUICulture 并强制更改绑定,转换器将使用新的文化。
如果您想以长格式显示日期,请在 XAML 中声明转换器,如下所示:
<local:StringFormatter x:Key="LongDateFormatter" Format=" {0:D}" />
然后像这样在你的 TextBlock 中使用它:
<TextBlock x:Name="DateText" Text="{Binding DateTime.Date, Converter={StaticResource LongDateFormatter}, Mode=OneWay}"/>
在代码中,您可以执行以下操作来强制更改绑定:
Thread.CurrentThread.CurrentUICulture = new CultureInfo(desiredCultureString);
var tempDateTime = this.DateTime;
this.DateTime = default(DateTime);
this.DateTime = tempDateTime;
当然还有其他方法可以强制改变,可能你也需要改变其他领域以适应新的文化,但这是如何处理它的一般思路。