CellStyle基于WPF中的RowStyle

Vla*_*zki 5 c# wpf xaml wpfdatagrid wpf-style

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

我需要的是这样的东西,虽然这不起作用,因为它说:

目标类型'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>
Run Code Online (Sandbox Code Playgroud)

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

我使用MyCellStyle这样的:

<dxg:GridColumn Header="My Header"
                FieldName="MyFieldName"
                Width="100"
                CellStyle="{StaticResource MyCellStyle}" />
Run Code Online (Sandbox Code Playgroud)

MyGridRowStyle像这样的TableView:

RowStyle="{StaticResource MyGridRowStyle}"
Run Code Online (Sandbox Code Playgroud)

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

WPF*_*any 2

基于普通的 WPF DataGrid,您可以尝试此操作并将其扩展为派生自的dxg 类(即来自 的子级)。该类派生自.DataGridCellContentControlContentDataGridRowControl

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

<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>
Run Code Online (Sandbox Code Playgroud)

摘要:为您 使用两者的基类型RowColumnBaseStyle,并将其用作BasedOn. 因为dxg您可以自己扩展它...