简体   繁体   中英

How can I change the background color if cells are selected?

I have a DataGrid , contains the following CellStyle for a column:

<DataGridTextColumn.CellStyle >
   <Style TargetType="DataGridCell">
      <EventSetter Event="MouseUp" Handler="DataGridCell_PreviewMouseUp"/>
      <Setter Property="ToolTip">
         <Setter.Value>
            <ToolTip>
               <StackPanel>
                  <TextBlock Text="{Binding FirstWeek.Monday.Dienstbezeichnung}"/>
                  <TextBlock Text="{Binding FirstWeek.Monday.Comment}" Visibility="{Binding FirstWeek.Monday.Comment,Converter={StaticResource ContentVisibilityConverter}}"/>
               </StackPanel>
            </ToolTip>
         </Setter.Value>
      </Setter>
      <Style.Triggers>
         <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
               <Condition Binding="{Binding FirstWeek.Monday, Converter={StaticResource CheckCellItem} }" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="{Binding FirstWeek.Monday.ContentColor, Converter={StaticResource ColorStringToColorConverter}}"></Setter>

         </MultiDataTrigger>
      </Style.Triggers>
   </Style>
</DataGridTextColumn.CellStyle>

As you can see, I use a DataTrigger to change the background color. My problem is that I didn't find a way to override this value if the cell is selected. If the user selects the cell, the background color should be blue as long as the cell is selected. Any ideas on how to solve this? Currently, only the borders of the cells change a little bit, but I want the whole cell to be blue, if it is selected.

Add a condition to your MultiDataTrigger , that ensures that the Setter is only applied if the current cell is not selected . Consequently, if a cell is selected, the default selection color is applied.

<MultiDataTrigger>
   <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding FirstWeek.Monday, Converter={StaticResource CheckCellItem} }" Value="True"/>
      <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="False"/>
   </MultiDataTrigger.Conditions>
   <Setter Property="Background" Value="{Binding FirstWeek.Monday.ContentColor, Converter={StaticResource ColorStringToColorConverter}}"/>
</MultiDataTrigger>

If you want to change the selection color in general, you have to create a custom control template and change the Selected state. You cannot do this in a style.

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