RowStyle-based CellStyle in WPF

I have a WPF DataGrid presented in XAML . I use RowStyle for my TableView grid, but it is also necessary to set some properties for specific cells. I need these cells to have row style properties and apply additional properties from the cell style on top of them.

I need something like this, although this does not work as it says:

The target type "CellContentPresenter" is not converted to the 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 also did not try to specify the BasedOn property for MyCellStyle , but this does not work either.

I use MyCellStyle as follows:

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

and MyGridRowStyle , as shown in TableView :

 RowStyle="{StaticResource MyGridRowStyle}" 

How to make a cell style change the properties specified in MyCellStyle and use the values ​​specified in MyGridRowStyle for other properties?

+5
source share
3 answers

based on a regular WPF DataGrid, you can try this and extend it for dxg the DataGridCell class is derived from ContentControl (this is a child of Content ). the DataGridRow class is derived from Control .

Now you can 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 the base type of the Row and Column classes for your BaseStyle and use it as BasedOn . For dxg you can expand it with your own ...

+2
source

You cannot create a CellContentPresenter style in the GridRowContent style. These are two completely different types, and only because they can have some properties that have the same name, they are still completely different and independent properties without being related to each other.

The best you can do is to define common values ​​as separate resources and use these resources in both styles, for example:

 <Window.Resources> <s:Double x:Key="commonHeight">25</s:Double> <SolidColorBrush x:Key="commonBg">Red</SolidColorBrush> <Style x:Key="MyCellStyle" TargetType="{x:Type dxg:CellContentPresenter}"> <Setter Property="Height" Value="{StaticResource commonHeight}" /> <Setter Property="Background" Value="{StaticResource commonBg}" /> </Style> <Style x:Key="MyGridRowStyle" BasedOn="{StaticResource {themes:GridRowThemeKey ResourceKey=RowStyle}}" TargetType="{x:Type dxg:GridRowContent}"> <Setter Property="Height" Value="{StaticResource commonHeight}" /> <Setter Property="Background" Value="{StaticResource commonBg}" /> </Style> </Window.Resources> 

But you still need to define all setters in both styles.

+4
source

My understanding of the question: cell style values ​​should vary depending on the style values ​​of the row in which it is located.

Turn off value

Here is a simple working example (check it by changing BackgroundGridRow Background to Red, you will notice that the front panel of the cell changes to blue):

  <DataGrid> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="Background" Value="White"/> </Style> </DataGrid.RowStyle> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}, Path=Background}" Value="Red"> <Setter Property="Foreground" Value="Blue"/> </DataTrigger> </Style.Triggers> </Style> </DataGrid.CellStyle> </DataGrid> 

You can perform a similar binding to directly set the CellStyle properties to the value of the properties of the row in which it is located.

Relative binding directly to the property

 <DataGrid x:Name="dataGrid1" ItemsSource="{Binding Collection}"> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="Height" Value="20"/> </Style> </DataGrid.RowStyle> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="FontSize" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow, Mode=FindAncestor}, Path=Height}"/> </Style> </DataGrid.CellStyle> </DataGrid> 

Explanation

RelativeBinding works because DataGridCells are the final children of DataGridRows, as you can see in this screenshot of the DataGrid visual tree:

DataGridCells are the ultimate children of DataGridRows

+2
source

Source: https://habr.com/ru/post/1260586/


All Articles