wpf - 如何在自定义 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:Key
DataGridRow 样式的值,然后在我的自定义数据网格样式中使用它,Setter Property
也没有任何更改。
解决方案
您可以在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>
推荐阅读
- python - dict中数据帧的大小
- android - 将参数添加到启动意图
- xml - lnamest 和 nameend 变成 colspan
- javascript - JS:将焦点设置在 DOM 元素上不会更新 document.activeElement
- mysql - 全选,包括空值和非空值
- javascript - WebCrypto 承诺似乎没有被调用
- passwords - TYPO3 9.5 - 需要帮助:fe_users 转换旧的 md5 密码
- php - PHP 错误:count() throws 参数必须是数组或实现 Countable 的对象
- selenium - DevToolsActivePort 文件不存在 Selenium Python
- javascript - 注释掉 `alert` 停止函数执行