在 WPF DataGrid 控件中设置选定单元格的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12831173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:43:36  来源:igfitidea点击:

Styling Selected Cells in the WPF DataGrid Control

c#wpfxamlstyles

提问by kevinarpe

I am trying to change the style for selected cells the WPF DataGrid control.

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

What is the difference between styling DataGridCelland DataGridRow? I tried various combinations below and they all work. However, it seems like I only need to style eitherDataGridCellor DataGridRow.

样式DataGridCell和 和有DataGridRow什么不一样?我尝试了下面的各种组合,它们都有效。然而,好像我只需要在风格无论是DataGridCellDataGridRow

What is the purpose of having both? This tells me I am misunderstanding their purpose.

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

Style for DataGridCell: (in my own code, I change the colors from the defaults)

样式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>

Style for DataGridRow: (in my own code, I change the colors from the defaults)

样式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>

回答by abhishek

DataGridRow will be applied to the whole Row. If you want to set any property of the whole row, you can use DataGridRow.

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

Each DataGridRow contains a list of DataGridCell. You can define a style for that too. If you dont, it will take up the style from the DataGridRow.

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

Generally, we specify the latter to define the differentiation between two adjacent cells, like specifying a margin between cells, borders etc.

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

回答by Muhammad Alaa

as @abhishek said ... I have a situation that I need to use both row style and cell style that is my row style

正如@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>

then I need specific cells to be white background while selected I used

然后我需要特定的单元格在选择时为白色背景我使用

<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>

and give the specific columns the style

并为特定列指定样式

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

I hope that is clear what I want to say

我希望我想说的很清楚