我嘗試構建一個DataGrid,並且我想將TextColums的Foreground屬性之一綁定到Date,以便它在Date變爲過去時變爲紅色。綁定WPF DataGrid文本列的前景
這裏的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}" />
這裏我轉換:
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
}
我不知道什麼是錯的,但轉換器甚至沒有caled(第一轉換器完美的作品)。輸出窗口顯示了這個:
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')
我希望你能幫助我,夥計們
THX
但我也希望「Prüfdatum」的內容(與我同意的參數,是那朵只有一個測試) – Tokk 2010-06-25 14:42:25
你可以在轉換器中設置一個斷點,看看你是否得到了價值? – Ragunathan 2010-06-29 05:06:32
這就是我發現轉換器沒有在運行時調用的方式 – Tokk 2010-06-29 14:45:17