1

我尝试将DateTimeFormatCultureConverter实施到DataGridTextColumn.

它可以工作,我可以调试它,但它根本不会改变 DateTime 格式。所以我看不到任何明显的变化......

(我总是可以使用return formated; // DateTime.Parse(formated); 但在这种情况下按 ASC/DESC 对字段进行排序不起作用。)

任何线索为什么?

谢谢!

代码

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" />
4

1 回答 1

2

您正在返回一个DateTime变量,而不是格式化的字符串。删除return DateTime.Parse(formated);, 而return formated;. 那应该对你有用。

编辑不使用值转换器,您可以改用StringFormatBinding 属性。请参阅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" />
于 2012-08-20T14:48:20.940 回答