【问题标题】:FormatString on WPF-DataGrid has no effectWPF-DataGrid 上的 FormatString 无效
【发布时间】:2013-09-03 07:19:34
【问题描述】:

我的 WPF-DataGrid 中的 FormatString 有一个小问题。我使用 DataGridTextColumn 作为列。我的数据源是IList<IDictionary<string, string>>

我的问题是,如果我设置StringFormat,它对列没有任何影响。

这是我的绑定代码:

cColumn.Binding = new Binding("[" + Config.InternName.ToLower() + "]");

这就是我设置 FormatString 的方式:

    #region [Date-Time Formating]
    if (Config.DateTimeFormat.Trim() != "")
    {
        string cDateTimeFormat = Config.DateTimeFormat.Trim();
        (cColumn.Binding as Binding).StringFormat = cDateTimeFormat;
    }
    #endregion

我尝试了很多 DateTime-StringFomrats,但都没有解决我的问题。

谢谢!

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    这是我的解决方案: 我不能简单地使用 FormatString,而不绑定日期时间。所以我写了一个IValueconverter:

    public class StringDateTimeValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime cOutDateTime = DateTime.Now;
    
            if (DateTime.TryParse(value.ToString(), out cOutDateTime))
            {
                return cOutDateTime;
            }
            else
            {
                return DependencyProperty.UnsetValue;
            }
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 2023-04-08
      • 2011-06-28
      • 2013-12-27
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多