2013-09-03 2 views
0

У меня небольшая проблема с FormatString в моем WPF-DataGrid. Я использую DataGridTextColumn для столбца. Мой DataSource является IList<IDictionary<string, string>>FormatString в WPF-DataGrid не имеет эффекта

Моя проблема в том, что если я установил 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, но ничего не решить мою проблему.

Спасибо!

ответ

0

Это мое решение: Я не могу просто использовать 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(); 
    } 
} 
Смежные вопросы