0
votes

i have a small problem with FormatString in my WPF-DataGrid. I use DataGridTextColumn for the column. My DataSource is a IList<IDictionary<string, string>>

My problem is, that if i set StringFormat, it doesn't have any effect on the column.

This is my binding code:

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

And this is how i set my FormatString:

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

I have tried a lot DateTime-StringFomrats, but nothing solve my problem.

Thank you!

1

1 Answers

0
votes

Here is my solution: I can't simple use FormatString, without binding a datetime. So i wrote an 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();
    }
}