views:

35

answers:

1

Hey, I try to built a DataGrid, and I want to bind one of the TextColums' Foreground property to a Date, so that it becomes red, if the Date is in the past.

Here the XAML:

<toolkit:DataGridTextColumn 
   Binding="{Binding Path=Prüfdatum, Converter={StaticResource TimestampToDateConverter}}"
   Header="Prüfdatum" 
   Foreground="{Binding Path=Prüfdatum, Converter={StaticResource TimestampToColorConverter},
   ConverterParameter=Prüfdatum}" />

Here my Converter:

    class TimestampToColorConverter: IValueConverter
{
    #region IValueConverter Member

    public object Convert(object value, Type targetType, 
           object parameter, System.Globalization.CultureInfo culture)
    {
        string Datum = value.ToString();
        int year = System.Convert.ToInt32(Datum.Substring(6, 4));
        int month = System.Convert.ToInt32(Datum.Substring(3, 2));
        int day = System.Convert.ToInt32(Datum.Substring(0, 2));
        int hour = System.Convert.ToInt32(Datum.Substring(11, 2));
        int minute = System.Convert.ToInt32(Datum.Substring(14, 2));
        int second = System.Convert.ToInt32(Datum.Substring(17, 2));
        DateTime Time = new DateTime(year, month, day, hour, minute, second);
        if (Time < System.DateTime.Now)
        {
            return Brushes.Red as Brush;
        }
        else
        {
            return Brushes.Black as Brush;
        }

    }

    public object ConvertBack(object value, Type targetType, 
         object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

    #endregion
}

I don't know what is wrong, but the Converter is not even caled (The first Converter works perfectly). The output window shows this:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Prüfdatum; DataItem=null; target element is
'DataGridTextColumn' (HashCode=16187528); target property is 'Foreground' (type 'Brush')

I hope you can help me, guys

Thx

A: 

Since you have binded to DataGrid so no need of "Binding" in DataGridTextColumn and ConverterParameter also not needed because your converter is not using the parameter. Try below

<toolkit:DataGridTextColumn  Header="Prüfdatum" Foreground="{Binding Path=Prüfdatum, Converter={StaticResource TimestampToColorConverter}}" />
Ragunathan
But I also want the "Prüfdatum" as Content (with the parameter I agree, tis was only a Test)
Tokk
Can you set a breakpoint in converter and see whether you are getting the value?
Ragunathan
That's the way I found out that the converter is not called at runtime
Tokk
Can i have code where you are binding the DataContext for this control or DataGrid?
Ragunathan