我有一个用 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}" />
和 MyGridRowStyle
在 TableView
上像这样:
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/