【问题标题】:DateTime Format Culture Converter日期时间格式文化转换器
【发布时间】:2012-08-20 14:27:07
【问题描述】:

我尝试将 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" />

【问题讨论】:

    标签: c# .net silverlight xaml


    【解决方案1】:

    您返回的是DateTime 变量,而不是格式化的字符串。删除return DateTime.Parse(formated);,取而代之的是return formated;。这应该对你有用。

    编辑 不使用值转换器,您可以改用StringFormat Binding 属性。见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" />
    

    【讨论】:

    • 正确。我总是可以使用格式化的返回; // DateTime.Parse(格式化);但在这种情况下,按 ASC/DESC 对字段进行排序是行不通的。
    • 啊。在您的问题中错过了这一点。
    • 嗯.. 我试过 Binding="{Binding Installed, StringFormat='{}{0:d}'" 但它不起作用。
    • 错误是什么?我认为不需要单引号。
    • 嗯...如果没有单引号,则会出现语法错误。所以它不会构建应用程序。
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多