I'm looking to compare databound cells in the same row of data to each other and highlight in red if they are different the cells that they are in.
If I statically provide a value for the conversion the cell is highlighted correctly, but I cant for the life of me figure out how to compare two cells of data in the same row. The below doesn't work as the Value parameter wont accept a databound item. This is my first foray into WPF so please be kind ;)
Can anybody advise how I can do a comparison of two items on the same row of data?
<DataGridTextColumn x:Name="oldContainerNumberColumn" Binding="{Binding OldContainerNumber}" Header="Old Container Number" IsReadOnly="True" Width="SizeToHeader">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding OldContainerNumber,Converter={StaticResource StringComparisonConverter},ConverterParameter={Binding NewContainerNumber}}" Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
You can't bind to ConverterParameter
because it's not a DependencyProperty
.
You could use a MultiBinding
within the DataTrigger
:
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource StringComparisonConverter}">
<Binding Path="OldContainerNumber" />
<Binding Path="NewContainerNumber" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</DataTrigger>
Of course you have to change your StringComparisonConverter to an IMultiValueConverter.
I think simpler solution would be to add a new property doing the comparison. Something like in this sample:
class YourClass
{
public int OldContainerNumber { get; set; }
public int NewContainerNumber { get; set; }
public bool IsEqual
{
get { return OldContainerNumber == NewContainerNumber; }
}
}
No need for a Converter:
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEqual}" Value="True">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>