所选DataGridRow的背景颜色不起作用/如何与AlternationIndex结合?

时间:2014-05-21 06:40:58

标签: wpf xaml

我尝试设置DataGrid的样式,这也包括样式DataGridRow。我想要的是在我的行上具有交替的背景行为(例如,白色/灰色);选择行时,背景应为YellowGreen。不知何故,以下代码无效,但我确信使用DataGridCell可以使选择部分工作;我只是想知道为什么不这样做:

  <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
      <Trigger Property="DataGridRow.AlternationIndex" Value="0">
        <Setter Property="Background" Value="{StaticResource EnBWLichtgrau}"/>
      </Trigger>
      <Trigger Property="DataGridRow.AlternationIndex" Value="1">
        <Setter Property="Background" Value="White"/>
      </Trigger>
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="DataGridRow.AlternationIndex" Value="0"/>
          <Condition Property="DataGridRow.IsSelected" Value="True"/>
        </MultiTrigger.Conditions>
        <MultiTrigger.Setters>
          <Setter Property="Foreground" Value="Red"/>
          <Setter Property="Background" Value="Pink"/>
        </MultiTrigger.Setters>
      </MultiTrigger>
    </Style.Triggers>
  </Style>
  <Style TargetType="{x:Type DataGrid}">
    <Setter Property="AlternationCount" Value="2"/>
    <Setter Property="BorderBrush" Value="{StaticResource EnBWAnthrazitgrau}"/>
    <Setter Property="HeadersVisibility" Value="Column"/>
  </Style>

也许有些setter会覆盖MultiTrigger setter,也许是其他东西,我不知道。 MultiTrigger现在并不是必需的,但即使是IsSelected上的简单Trigger也不会这样。

正如我所说,我可能会去设计DataGridCell,但是&#34;为什么&#34;是我感兴趣的:-)谢谢!

编辑:我尝试使用DataGridCell代替IsSelected部分,它按预期工作。但是,如果我希望所选行背景颜色不同,具体取决于单元格所在的DataGridRow的AlternationIndex?有没有办法在DataGridCell样式定义中获取此索引的值,例如。?

0 个答案:

没有答案