简体   繁体   中英

Styling Selected Cells in the WPF DataGrid Control

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

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

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)

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

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

DataGridRow will be applied to the whole Row. If you want to set any property of the whole row, you can use 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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM