2017-05-19 52 views
1

我正在使用HeaderedItemsControl。每个项目是一个3列的网格,每列中都有一个边框和TextBlock。我希望每个项目中边界的背景颜色交替出现。 (基本交替行的背景效果)。我试图在为应用背景颜色,在它所有的边界网格中的用户控件级别创建一个风格的基础上,包含控制AlternationIndex:如何根据父级的AlternateIndex设置孩子的风格?

<Style TargetType="Grid" x:Key="myItemsGrid"> 
    <Style.Resources> 
    <Style TargetType="Border"> 
     <Setter Property="Background" Value="Azure" /> 
     <Style.Triggers> 
     <DataTrigger Binding="{Binding Path=AlternationIndex, RelativeSource={RelativeSource AncestorType=ItemsControl}}" Value="2"> 
      <Setter Property="Background" Value="{StaticResource color_LogoLight}" /> 
     </DataTrigger> 
     </Style.Triggers> 
    </Style> 
    </Style.Resources> 
</Style> 

的Setter位正在工作,因为边界都是“Azure”。但是,如何正确引用AlternationIndex,以便每隔一行更改边框背景颜色。我试着将RelativeSource指向HeaderedItemsControl和ItemsControl,但都不是正确的目标。我浏览了活的视觉树,但我找不到任何可以参考的地方。

任何帮助表示赞赏。

回答

0

您必须在ItemControl的项目上查找AlternationIndex,而不是在ItemsControl本身上!但是你必须搜索绑定的类型?例如,在ListBox中它是一个ListBoxItem,在ItemsControl中它是一个ContentPresenter
不要忘记Path=(ItemsControl.AlternationIndex)和你的情况(AlternationIndex == 2)你必须AlternationCount设置在ItemsControl中至少!所以这个代码应该工作:

<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ContentPresenter}}" Value="2"> 
    <Setter Property="Background" Value="{StaticResource color_LogoLight}" /> 
</DataTrigger> 
+0

有两件事... ContentPresenter有点失踪。我理解了这个概念,但我无法辨别在这种情况下作为“ListBoxItem”的行为。而且,出于某种原因,括号会有所作为,因为没有它们就无法运作。很有帮助。感谢您的答复。 –