How can I set the color of a selected row in DataGrid

后端 未结 8 1554

The default background color of a selected row in DataGrid is so dark that I can\'t read it. Is there anyway of overriding it?

Tried this



        
相关标签:
8条回答
  • 2020-11-28 20:29

    Some of the reason which I experienced the row selected event not working

    1. Style is set up for DataGridCell
    2. Using Templated columns
    3. Trigger is set up at the DataGridRow

    This is what helped me. Setting the Style for DataGridCell

    <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
          <Setter Property="Background" Value="Green"/>
          <Setter Property="Foreground" Value="White"/>
        </Trigger>
      </Style.Triggers> 
    </Style>
    

    And since I was using a template column with a label inside, I bound the Foreground property to the container Foreground using RelativeSource binding:

    <DataGridTemplateColumn>
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <Label Content="{Binding CategoryName,
                     Mode=TwoWay,
                     UpdateSourceTrigger=LostFocus}"
                 Foreground="{Binding Foreground,
                     RelativeSource={RelativeSource Mode=FindAncestor,
                         AncestorLevel=1, 
                         AncestorType={x:Type DataGridCell}}}"
                 Width="150"/>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    
    0 讨论(0)
  • 2020-11-28 20:31

    Got it. Add the following within the DataGrid.Resources section:

      <DataGrid.Resources>
         <Style TargetType="{x:Type dg:DataGridCell}">
            <Style.Triggers>
                <Trigger Property="dg:DataGridCell.IsSelected" Value="True">
                    <Setter Property="Background" Value="#CCDAFF" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
    
    0 讨论(0)
提交回复
热议问题