I have a DataGridTemplateColumn that defines a TextBlock which has bound Background and Foreground properties. This allows the colors to change based on the value of the bound property. So far so good, except I want the default selected row color to override my bound background color. How can I do this in xaml?
<DataGridTemplateColumn Header="Text">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Text}"
Background="{Binding Path=BackgroundColor}"
Foreground="{Binding Path=ForegroundColor}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Ultimately, it seems like I need to determine if the cell is in the selected row. If so, use the default selected row background color else use the bound background color. I am not sure how to aproach this. Any help would be appreciated.
You could refrain from directly binding the
Backgroundto instead assign aStyleto theTextBlockwhich uses aDataTriggeron the selection ({Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}) beingfalseand only then sets theBackgroundto the binding.