基于WPF中的行样式创建单元格样式

5
我有一个用XAML表示的WPF DataGrid。我为我的网格的TableView使用了RowStyle,但还需要为特定单元格设置一些属性。我需要这些单元格具有行样式的属性,并在其上应用单元格样式的额外属性。
我需要的是像这样的东西,尽管它不起作用,因为它说:“目标类型'CellContentPresenter'无法转换为基本类型'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>

我也尝试了不指定 MyCellStyleBasedOn 属性,但那也不起作用。
我是这样使用 MyCellStyle 的:
<dxg:GridColumn Header="My Header"
                FieldName="MyFieldName"
                Width="100"
                CellStyle="{StaticResource MyCellStyle}" />

TableView上,像这样使用MyGridRowStyle

RowStyle="{StaticResource MyGridRowStyle}"

如何使单元格样式仅更改MyCellStyle中指定的属性,并对其他属性使用MyGridRowStyle中指定的值?

3个回答

4

你不能基于GridRowContent样式来定义CellContentPresenter样式。这两者是完全不同的类型,即使它们可能具有一些相同名称的属性,这些属性仍然是完全不同且独立的,彼此之间没有任何关系。

最好的方法是将通用的定义为单独的资源,并在两个样式中使用这些资源, 如下:

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

但是你仍然需要在两种风格中定义所有的setters


2

基于普通的 WPF DataGrid,您可以尝试以下方法并将其扩展为 dxg

DataGridCell 类是从 ContentControl 派生的(它是 Content 的子类)。 DataGridRow 类是从 Control 派生的。

现在您可以尝试以下操作:

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

概述: 在你的BaseStyle中使用RowColumn类的基础类型,并将其用作BasedOn。对于dxg,您可以通过自己的方式进行扩展...


1
这是最简单的解决方案,正是我所寻找的。它非常有效,谢谢! - Vlad Schnakovszki

2

我理解这个问题的意思是:单元格样式的值应该根据所在行的样式值而改变。

基于值触发

以下是一个简单的工作示例(通过将DataGridRow Background更改为Red来测试它,您会注意到单元格的前景色更改为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>

您可以使用类似的绑定方式,将单元格样式(CellStyle)的属性直接设置为所在行(Row)的属性值。

直接相对绑定到属性

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

解释

RelativeBinding的工作原理是由于DataGridCells最终是DataGridRows的子元素,如下图所示:

DataGridCells are eventual children of DataGridRows


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接