简体   繁体   English

如果选择了单元格,如何更改背景颜色?

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

I have a DataGrid , contains the following CellStyle for a column:我有一个DataGrid ,包含一列的以下CellStyle

<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.如您所见,我使用DataTrigger来更改背景颜色。 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 .MultiDataTrigger添加一个条件,以确保仅在未选择当前单元格时才应用Setter 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.如果要更改一般选择颜色,则必须创建自定义控件模板并更改Selected状态。 You cannot do this in a style.你不能以一种风格来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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