Search code examples
c#wpfxamlwpfdatagridwpf-style

CellStyle based on RowStyle in WPF


I have a WPF DataGrid represented in XAML. I'm using a RowStyle for my grid's TableView but also need to set some properties for specific cells. I need those cells to have the properties of the row style and apply the extra properties from the cell style on top of those.

What I would need is something like this, although this doesn't work as it's saying:

Target type 'CellContentPresenter' is not convertible to base type 'GridRowContent'

<Style x:Key="MyGridRowStyle"
    BasedOn="{StaticResource {themes:GridRowThemeKey ResourceKey=RowStyle}}"
    TargetType="{x:Type dxg:GridRowContent}">
    <Setter Property="Height" 
        Value="25" />
        <Style.Triggers>
        ...
    </Style.Triggers>
</Style>

<Style x:Key="MyCellStyle" 
    BasedOn="{StaticResource MyGridRowStyle}" 
    TargetType="{x:Type dxg:CellContentPresenter}">
    <Style.Triggers>
        ...
    </Style.Triggers>
</Style>

I've also tried not specifying the BasedOn property for MyCellStyle but that doesn't work either.

I use the MyCellStyle like this:

<dxg:GridColumn Header="My Header"
                FieldName="MyFieldName"
                Width="100"
                CellStyle="{StaticResource MyCellStyle}" />

and MyGridRowStyle like this on the TableView:

RowStyle="{StaticResource MyGridRowStyle}"

How can I make the cell style only change the properties specified in MyCellStyle and use the values specified in MyGridRowStyle for the other properties?


Solution

  • based on normal WPF DataGrid you could try this and expand it for dxg the class DataGridCell is derived from ContentControl (that is a child from Content). the class DataGridRow is derived from Control.

    now you could try the following:

    <Style x:Key="BaseStyle" TargetType="Control" >
        <!-- Maybe add BaseStyle / theme here with BasedOn -->
        <Setter Property="Height" Value="25" />
        <!-- Row and Column defaults -->
    </Style>
    <Style x:Key="MyGridRowStyle" BasedOn="{StaticResource BaseStyle}"
           TargetType="DataGridRow">
        <!-- Row specific implementation -->
    </Style>
    <Style x:Key="MyCellStyle" BasedOn="{StaticResource BaseStyle}"
           TargetType="DataGridCell">
        <!-- Column specific implementation -->
    </Style>
    

    summary: use base type of both Row and Column classes for your BaseStyle and use this one as BasedOn. For dxg you can extend it by your own...