c# - WPF中基于RowStyle的CellStyle

标签 c# wpf xaml wpfdatagrid wpf-style

我有一个用 XAML 表示的 WPF DataGrid。我正在为网格的 TableView 使用 RowStyle,但还需要为特定单元格设置一些属性。我需要这些单元格具有行样式的属性,并在这些单元格之上应用单元格样式的额外属性。

我需要的是这样的东西,虽然这不像它所说的那样有效:

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>

我也尝试过不为 MyCellStyle 指定 BasedOn 属性,但这也不起作用。

我像这样使用 MyCellStyle:

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

MyGridRowStyleTableView 上像这样:

RowStyle="{StaticResource MyGridRowStyle}"

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

最佳答案

您不能将 CellContentPresenter 样式基于 GridRowContent 样式。这是两种完全不同的类型,只是因为它们可能碰巧有一些同名的属性,所以它们仍然是完全不同的独立属性,彼此之间没有任何关系。

您可以做的最好的事情是将通用定义为单独的资源,并以两种样式使用这些资源,例如:

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

但是您仍然需要在两种样式中定义所有setter

关于c# - WPF中基于RowStyle的CellStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40889382/

相关文章:

wpf - 在 WPF 中自定义上下文菜单

c# - 正确使用兼容WPF的DoEvents()

.net - 从 DataTemplate 访问父 DataContext

WPF 松散 XAML 资源字典

c# - WPF ComboBox 到 ObservableCollection 的绑定(bind)

c# - WPF 新增功能 - 更新代码中的 TextBlock 导致 NullReferenceException

c# - 抛出异常后继续循环迭代

c# - Visual Studio 中的大括号默认布局

c# - 如何在 C# 中嵌入 XML 文件并对其进行反序列化?

c# - 如何在不破坏动画的情况下重命名生物模型的层次结构?