2

我正在尝试更改 WPF DataGrid 控件的选定单元格的样式。

styleDataGridCell和 和有什么不一样DataGridRow?我在下面尝试了各种组合,它们都有效。但是,似乎我只需要设置样式DataGridCellDataGridRow

两者兼有的目的是什么?这告诉我我误解了他们的目的。

样式DataGridCell:(在我自己的代码中,我更改了默认颜色)

 <Style TargetType="{x:Type DataGridCell}">
 <Style.Triggers>
  <Trigger Property="IsSelected" Value="True">
  <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  </Trigger>
 </Style.Triggers>
 </Style>

样式DataGridRow:(在我自己的代码中,我更改了默认颜色)

 <Style TargetType="{x:Type DataGridRow}">
 <Style.Triggers>
  <Trigger Property="IsSelected" Value="True">
  <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  </Trigger>
 </Style.Triggers>
 </Style>
4

2 回答 2

4

DataGridRow 将应用于整个 Row。如果要设置整行的任何属性,可以使用 DataGridRow。

每个 DataGridRow 都包含一个 DataGridCell 列表。您也可以为此定义样式。如果您不这样做,它将采用 DataGridRow 中的样式。

通常,我们指定后者来定义两个相邻单元格之间的差异,例如指定单元格之间的边距、边界等。

于 2012-10-11T02:26:03.140 回答
2

正如@abhishek 所说......我有一种情况,我需要同时使用行样式和单元格样式,这是我的行样式

<!-- Row Style-->
        <Style x:Key="RowStyle" TargetType="{x:Type dg:DataGridRow}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

然后我需要在选择时将特定单元格设为白色背景

<Style x:Key="TimeCell" TargetType="{x:Type dg:DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

并为特定列指定样式

<dg:DataGridTemplateColumn Width="120" CellStyle="{StaticResource TimeCell}">

我希望我想说的很清楚

于 2013-12-07T14:27:17.880 回答