I've a DataGrid and I want to change the background colours of individual cells. This is reasonably simple to do after some searching with xaml such as
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="Border.Background" Value="{Binding Converter={StaticResource ImportTableBackgroundColorConverter},ConverterParameter=GotName}" />
</Style>
</DataGridTextColumn.CellStyle>
However, in an app-wide ResourceDictionary I also have
<Style TargetType="DataGrid" x:Key="GlobalCellStyle">
<!-- Cell style -->
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="DataGridCell">
<!-- Single Click Editing -->
<EventSetter Event="PreviewMouseLeftButtonDown"
Handler="DataGridCell_PreviewMouseLeftButtonDown" />
<EventSetter Event="KeyDown" Handler="DataGridCell_KeyDown" />
<EventSetter Event="GotFocus" Handler="DataGridCell_GotFocus"/>
<!--body content datagrid cell vertical centering-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
This sets all DataGrid cells to centre their content and, using some codebehind that goes with the same file, makes the cells go into edit mode on a single click. Specifying a new style locally loses this. If I try and specify the new local style based on the global, I get the exception Can only base on a Style with target type that is base type 'IFrameworkInputElement'
.
I've tried bringing the global DataGridCell style itself outside the global DataGrid style and get the same error. This is despite DataGridCell appearing to implement IFrameworkInputElement.
Because I'm passing a parameter to the ValueConverter to let it identify which field the cell is displaying, I can't move my background colour stuff to the global style- I'd have to have the whole row background changing colour together. And copying the global style to each column declaration in my table, as well as possibly having to copy the codebehind as well, seems quite horrendous both initially and to maintain later.
Does anyone know how I can either get the style inheritance to work, or to know what column I'm in when the ValueConverter is called?