如何通过 MultiDataTrigger 为 WPF DataGrid 上的选定行设置替代背景色

How to have alternative background color of the selected rows on WPF DataGrid via MultiDataTrigger

我想在 WPF DataGrid 中显示具有不同背景颜色(灰色)的奇数行和偶数行。我已经使用 RowBackground="#DDD"AlternatingRowBackground="#EEE"AlternationCount=2 属性以及 DataGrid 本身来交替未选定行的颜色。

我已经在 Whosebug 上浏览了很多线程,但找不到 Style.Triggers 无效的原因。我用这个attached property判断是奇数还是偶数行

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        
        <!-- Changes the row style template to have more advanced "borders" (dotted, dashed, etc.) based on value converters -->
        <Setter Property="Template">
            <!-- ... -->
        </Setter>
        
        <!-- Changes the background color of odd rows (will do the same for even one with a slightly different color) -->
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}"
                               Value="True"/>
                    <Condition Binding="{Binding (behaviors:DataGridBehavior.IsOddRow), RelativeSource={RelativeSource Self}}"
                               Value="True"/>
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.Setters>
                    <Setter Property="Background"
                            Value="LightYellow"/>
                </MultiDataTrigger.Setters>
            </MultiDataTrigger>

            <!-- Same principle for even rows -->
            <!-- ... -->

        </Style.Triggers>
    </Style>
</DataGrid.RowStyle>

如果我在 MutliDataTrigger.

中只使用一个 Condition,效果相同(没有)

感谢您的任何见解:-)

您不必为此使用自定义行为,您可以使用 built-in 交替机制。

<DataGrid AlternationCount="2"
          ...>
   <!-- ...other properties -->
</DataGrid>

DataGridRow 创建样式,使用 IsSelected 的触发器和 DataGrid 上的 AlternationIndex attached property. Note, that we have to set the row background color and the alternating background color here, too, because setting the RowBackground and AlternatingRowBackground 将覆盖样式中定义的背景值,并且选择突出显示不会工作,所以将它们从 DataGrid.

中删除
<DataGrid.RowStyle>
   <Style TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">

      <!-- Changes the row style template to have more advanced "borders" (dotted, dashed, etc.) based on value converters -->
      <Setter Property="Template">
         <!-- ... -->
      </Setter>

      <!-- Part for odd rows. -->
      <Style.Triggers>
         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsSelected"
                          Value="False"/>
               <Condition Property="AlternationIndex"
                          Value="1"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
               <Setter Property="Background"
                       Value="#EEE"/>
            </MultiTrigger.Setters>
         </MultiTrigger>

         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsSelected"
                          Value="True"/>
               <Condition Property="AlternationIndex"
                          Value="1"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
               <Setter Property="Background"
                       Value="Yellow"/>
            </MultiTrigger.Setters>
         </MultiTrigger>

         <!-- Part for even rows. -->

         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsSelected"
                          Value="False"/>
               <Condition Property="AlternationIndex"
                          Value="0"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
               <Setter Property="Background"
                       Value="#DDD"/>
            </MultiTrigger.Setters>
         </MultiTrigger>

         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsSelected"
                          Value="True"/>
               <Condition Property="AlternationIndex"
                          Value="0"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
               <Setter Property="Background"
                       Value="LightYellow"/>
            </MultiTrigger.Setters>
         </MultiTrigger>

      </Style.Triggers>

   </Style>
</DataGrid.RowStyle>

因为 DataGridCellDataGridRow 的子项,那里定义的选择颜色将遮挡行颜色。为了避免为 DataGridCell 创建相同的样式,我们在选中它时将其背景设置为透明。

<DataGrid.CellStyle>
   <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
      <Style.Triggers>
         <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"
                      Value="True">
            <Setter Property="Background"
                    Value="Transparent"/>
         </DataTrigger>
      </Style.Triggers>
   </Style>
</DataGrid.CellStyle>