概括@Jakob Möllås 的答案:
问题与
<TextBlock Text="{Binding Date, StringFormat='Received On: {0}', ConverterParameter=shortdatewithyear, Converter={StaticResource DateTimeToTimeConvert}}"/>
是绑定以某种方式缓存了 StringFormat 的值,即使您在 DataGrid 中有一个 GroupStyle 并重新加载 DataGrid 的内容,它也不会被更新。使用后无法更改绑定(您会遇到异常)。所以解决方案是使用这样的转换器:
public class StringFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var formatString = parameter as string;
if(string.IsNullOrEmpty(formatString))
return null;
return string.Format(formatString, value);
}
// ...
}
然后在 XAML 中:
<!-- ... declare your converter at some place ... -->
<!-- then -->
<TextBlock Text="{Binding Date, Converter={StaticResource LocalizationConverter}, ConverterParameter={x:Static names:MyClass.LocalizedStringFormat}}"/>
因此,当您更新 MyClass.LocalizedStringFormat 的值时(也许您更改了程序中的显示语言),您需要做的就是为使用本地化 StringFormat 的属性抛出一个 PropertyChanged。
注意:转换器在每个 PropertyChanged 上执行,因此可能会或可能不会比使用带有 DataBinding 的 StringFormat 慢一些。