WPF中基于RowStyle的CellStyle [英] CellStyle based on RowStyle in WPF
问题描述
我有一个WPF
DataGrid
用XAML
表示.我为网格的TableView
使用了RowStyle
,但是还需要为特定的单元格设置一些属性.我需要这些单元格具有行样式的属性,并在其顶部应用单元格样式中的其他属性.
I have a WPF
DataGrid
represented in XAML
. I'm using a RowStyle
for my grid's TableView
but also need to set some properties for specific cells. I need those cells to have the properties of the row style and apply the extra properties from the cell style on top of those.
我需要的是这样的东西,尽管它不能正常工作:
What I would need is something like this, although this doesn't work as it's saying:
目标类型'CellContentPresenter'不能转换为基本类型'GridRowContent'
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
属性,但这也不起作用.
I've also tried not specifying the BasedOn
property for MyCellStyle
but that doesn't work either.
我这样使用MyCellStyle
:
<dxg:GridColumn Header="My Header"
FieldName="MyFieldName"
Width="100"
CellStyle="{StaticResource MyCellStyle}" />
和MyGridRowStyle
在TableView
上是这样的:
RowStyle="{StaticResource MyGridRowStyle}"
如何使单元格样式仅更改MyCellStyle
中指定的属性,并将MyGridRowStyle
中指定的值用于其他属性?
How can I make the cell style only change the properties specified in MyCellStyle
and use the values specified in MyGridRowStyle
for the other properties?
推荐答案
基于常规WPF DataGrid,您可以尝试将其扩展为dxg
类DataGridCell
是从ContentControl
派生的(这是Content
的子级).
类DataGridRow
是从Control
派生的.
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>
摘要:
对BaseStyle
使用Row
和Column
类的基本类型,并将其用作BasedOn
.对于dxg
,您可以自己扩展...
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...
这篇关于WPF中基于RowStyle的CellStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!