CellStyle based on RowStyle in WPF

做~自己de王妃 提交于 2019-12-05 11:31:02

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...

You can't base a CellContentPresenter style on a GridRowContent style. These are two completely different types and just because they may happen to have some properties that have the same name these are still completely different and independant properties with no relationship to each other.

The best thing you can do is to define the common values as separate resources and use these resources in both styles, e.g.:

<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.

My understanding of the question: values of the cell style should change based off of values in the style of the row it is in.

Trigger off of value

Here is a simple working example (test it by changing the DataGridRow Background to Red, you will notice that the cell's foreground 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 do a similar binding to directly set properties of the CellStyle to the value of properties of the row it is in.

Relative bind directly to a 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 due to the fact that DataGridCells are eventual children of DataGridRows as can been seen in this screenshot of the visual tree of a DataGrid:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!