So I would like to change the background color of any dataGridCell.
The DateGrid.ItemsSource is bound to a class something like this.
public class OldData
{
public Cell Name{ get; set; }
}
public class Cell
{
public object Value { get; set; }
public Boolean DoesMatch { get; set; }
}
I have been trying to set up a cell template with a trigger on DoesMatch, but wpf cannot seem to find it.
<DataGrid x:Name="dgNew" AutoGenerateColumns="True" >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DoesMatch, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type lib:Cell}}}" Value="False">
<Setter Property="Background" Value="#FFF35656" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
I have tried several variations of the template, but they spit out Data errors into my output window, and of coerce, my cell backgrounds don't change. :)
WPF Output Error:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='CsvDataCompareTool.Lib.Cell', AncestorLevel='1''. BindingExpression:Path=DoesMatch; DataItem=null; target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object')
Any help would be greatly appreciated.
Update:
tried it without relativeSource
<DataTrigger Binding="{Binding Path=DoesMatch}" Value="False">
<Setter Property="Background" Value="#FFF35656" />
</DataTrigger>
Error:
System.Windows.Data Error: 40 : BindingExpression path error: 'DoesMatch' property not found on 'object' ''NewData' (HashCode=53750044)'. BindingExpression:Path=DoesMatch; DataItem='NewData' (HashCode=53750044); target element is 'DataGridCell' (Name=''); target property is 'NoTarget' (type 'Object')
NewItem
objects which have a property of typeCell
called Name, right? – Peter Hansen