我尝试将 DateTimeFormatCultureConverter 实施到DataGridTextColumn
。
它可以工作,我可以调试它,但它根本不会改变DateTime格式。所以我看不到任何明显的变化......
(我总是可以使用return formated; // DateTime.Parse(formated);
但在这种情况下,按ASC / DESC排序字段不起作用。)
有什么线索的原因?
谢谢!
CODE
public class DateTimeFormatCultureConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
DateTime originalValue = (DateTime)value;
CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;
if (currentUICulture.EnglishName.Contains("Spanish") || currentUICulture.EnglishName.Contains("Portuguese"))
{
string formated = string.Format("{0}/{1}/{2}", originalValue.Day, originalValue.Month, originalValue.Year);
return DateTime.Parse(formated);
}
else
{
string formated = string.Format("{0}/{1}/{2}", originalValue.Month, originalValue.Day, originalValue.Year);
return DateTime.Parse(formated);
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
数据网格
<sdk:DataGridTextColumn x:Name="txcInstalled"
CanUserReorder="True"
CanUserResize="True"
CanUserSort="True"
Width="Auto"
Binding="{Binding Installed, Converter={StaticResource DateTimeFormatCultureConverter}}"
IsReadOnly="True" />
答案 0 :(得分:2)
您正在返回DateTime
变量,而不是格式化的字符串。移除return DateTime.Parse(formated);
,而不是return formated;
。那应该是你的伎俩。
编辑不使用值转换器,您可以使用StringFormat
绑定属性。见http://blogs.msdn.com/b/mikehillberg/archive/2008/05/29/trying-out-binding-stringformat.aspx
以下内容应该为您提供一个特定于文化的字符串,我想这就是您所希望的:
<sdk:DataGridTextColumn x:Name="txcInstalled"
CanUserReorder="True"
CanUserResize="True"
CanUserSort="True"
Width="Auto"
Binding="{Binding Installed, StringFormat={}{0:d}}"
IsReadOnly="True" />