我希望能够像这样设计网格样式:
<DataGrid Style="{DynamicResource BilagDatagridStyle}">
<DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
但我无法弄清楚设置datagridcell样式的正确语法,我在Property =&#34; DataGridCell.Style&#34;上收到错误。抱怨它与Property =&#34; DataGridRow.Style&#34;相同。错误是:重复属性&#39;样式&#39;设定器
<Style x:Key="DefaultReadOnlyGridStyle" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="CanUserSortColumns" Value="False"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="RowHeight" Value="30"/>
</Style>
<Style x:Key="BilagDatagridStyle" BasedOn="{StaticResource DefaultReadOnlyGridStyle}">
<Setter Property="DataGridCell.Style" Value="{DynamicResource DataGridRowNoInteractionStyle}"/>
<Setter Property="DataGridRow.Style" Value="{DynamicResource DataGridRowNoInteractionStyle}"/>
</Style>
我知道有办法做到这一点,但是怎么做?
答案 0 :(得分:1)
指定TargetType时,无需在属性名称中添加UIelement的名称。在你的情况下,适当的是CellStyle和RowStyle,但如果你摆脱TargetType,那么你必须使用DataGrid.CellStyle / DataGrid.RowStyle。
<Style x:Key="YourCellStyle" TargetType="DataGridCell">
</Style>
<Style x:Key="YourRowStyle" TargetType="DataGridRow">
</Style>
<Setter Property="CellStyle" Value="{StaticResource YourCellStyle}"/>
<Setter Property="RowStyle" Value="{StaticResource YourRowStyle}"/>