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