I am attempting to change the foreground of a cell based on the DateTime value from an object in the ItemsSource using a converter.
The issue that I am having is that when running the application I get an exception of 'GridCell' TargetType does not match type of element 'GridCell'.
The exception doesn't seem to make any sense since a GridCell TargetType is exactly the same as a GridCell element. I would greatly appreciate it if someone could point out what I am doing wrong. The code is below.
Model used in generating the ItemsSource (code behind creates a List. This all works fine)
public class RelationalTable
{
public int ClaimNumber { get; set; }
public DateTime PlanPosted { get; set; }
public DateTime PlanDue { get; set; }
public int PlanVersion { get; set; }
public string Department { get; set; }
}
SfDataGrid markup:
<UserControl.Resources>
<!-- converter is defined in the UserControl header xmlns:converter=<assembly holding the converter code> -->
<converter:StyleConverterByDate x:Key="ConverterResult" />
</UserControl.Resources>
<sf:SfDataGrid x:Name="SfGrid" ItemsSource="{Binding ClaimList}" AutoGenerateColumns="False"
IsReadOnly="True" AllowSorting="True" AllowFiltering="True"
FontFamily="{StaticResource AppFont}"
AllowEditing="False" AllowTriStateSorting="True" ColumnSizer="AutoWithLastColumnFill"
RowHeight="32">
<i:Interaction.Behaviors>
<loc:SfDataGridBehavior />
</i:Interaction.Behaviors>
<sf:SfDataGrid.Columns>
<sf:GridTextColumn HeaderText="Claim Number"
HorizontalHeaderContentAlignment="Left"
MappingName="ClaimNumber">
</sf:GridTextColumn>
<sf:GridDateTimeColumn CustomPattern="MM/dd/yyyy"
HorizontalHeaderContentAlignment="Left"
HeaderText="Date Posted"
MappingName="PlanPosted"
Pattern="CustomPattern" />
<sf:GridDateTimeColumn CustomPattern="MM/dd/yyyy"
HorizontalHeaderContentAlignment="Left"
HeaderText="Date Due"
MappingName="PlanDue"
Pattern="CustomPattern">
<sf:GridDateTimeColumn.CellStyle>
<Style TargetType="sf:GridCell">
<Setter Property="Foreground" Value="{Binding PlanDue, Converter={StaticResource ConverterResult}}" />
</Style>
</sf:GridDateTimeColumn.CellStyle>
</sf:GridDateTimeColumn>
<sf:GridTextColumn HeaderText="Department"
HorizontalHeaderContentAlignment="Left"
MappingName="Department" />
</sf:SfDataGrid.Columns>
</sf:SfDataGrid>
And finally the converter
public class StyleConverterByDate : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime _value = (DateTime)value;
DateTime _today = DateTime.Today;
if (_value.Date >= _today.Date)
return new SolidColorBrush(Colors.Red);
if (_value.Date > _today.Date.AddDays(-10) && _value.Date < _today.Date)
return new SolidColorBrush(Colors.Yellow);
return new SolidColorBrush(Colors.Black);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new SolidColorBrush(Colors.Black);
}
}
I found the solution. I replaced the CellStyle with a CellTemplate and used a TextBlock inside a DataTemplate to display the cell data.
The foreground color of the Date in the GridDateTimeColumn now changes at runtime based on the condition met in the converter.
Hope this helps someone else because I was stumped for a while on this one.