WPF DataGrid Text Column Foreground Linking

I am trying to create a DataGrid and I want to bind one of the Foreground TextColums properties to a date so that it turns red if the date is in the past.

Here is 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 is 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’s wrong, but the converter is not even a frame (the first converter works fine). The output window shows the following:

System.Windows.Data error: 2: Cannot find a FrameworkElement or FrameworkContentElement control for the target element. BindingExpression: Path = Prüfdatum; DataItem = NULL; target element is 'DataGridTextColumn' (HashCode = 16187528); target property "Foreground" (type "Brush")

Hope you help me guys.

thank

+3
3

DataGrid, "" DataGridTextColumn ConverterParameter , .

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

DataGridTextColumn DataGrid TextBlock . DataGridTextColumn.Binding TextBlock.Text, , TextBlock .

, - , (.. ):

Binding = "{Binding Path = Prüfdatum, Converter = {StaticResource TimestampToDateConverter}}"

- ForeGround. TextBlock. , TextBlock . DataGridRow, . DataGridRow.Item :

<Window.Resources>
  <Style x:Key="ForegroundStyle" TargetType="TextBlock">
    <Setter Property="Foreground" 
      Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
      Path=Item.Prüfdatum, 
      Converter={StaticResource TimestampToColorConverter}/>
  </Style>
</Window.Resources>

<DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding SomeData}" ElementStyle="{StaticResource ForegroundStyle}"/>
</DataGrid.Columns>

? datagrid : http://www.codeproject.com/Articles/683429/Guide-to-WPF-DataGrid-formatting-using-bindings?msg=5037235#xx5037235xx

, : -)

+1

DataGridTextColumn Foreground .

- . .

<DataGridTemplateColumn Header="Heading">
    <DataGridTemplateColumn.CellTemplate >
        <DataTemplate>
            <TextBlock Text="{Binding SomeData}" 
                Foreground="{Binding SomeData, Converter={StaticResource TimestampToColorConverter}}" />
       </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
0

Source: https://habr.com/ru/post/1751762/


All Articles