首页 > 解决方案 > 如何在自定义 DataGrid 上为 DataGridRow 设置样式和模板

问题描述

我有一个自定义 DataGrid 元素。Theme.xml 中描述的该元素的 Stle 和模板:

<Style TargetType="{x:Type local:MyDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
     <Setter Property="RowHeaderWidth" Value="0"/>
     <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                 <!-- ... -->
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>

如何为我的自定义 DataGrid 指定 DataGridRow 的模板。像这样的东西:

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridRow}">
                <Border BorderThickness="3">
                    <DataGridCellsPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

文学改变什么。

设置x:KeyDataGridRow 样式的值,然后在我的自定义数据网格样式中使用它,Setter Property也没有任何更改。

标签: wpfxaml

解决方案


您可以在RowStyle以下位置设置属性Style

<Style TargetType="{x:Type local:MyDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
    <Setter Property="RowHeaderWidth" Value="0"/>
    <Setter Property="Template">
        ...
    </Setter>
    <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="{x:Type DataGridRow}">
                ...
            </Style>
        </Setter.Value>
    </Setter>
</Style>

...或在MyDataGrid元素上:

<local:MyDataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            ...
        </Style>
    </DataGrid.RowStyle>
</local:MyDataGrid>

推荐阅读