0
我有一個ListBox
,我想它有根據交替指數2組完全不同的數據模板的樣式。我看過很多關於如何根據索引更改背景顏色但不更改每個索引樣式的教程。這是迄今爲止我所擁有的。列表框ContentControl中有兩個數據模板和觸發器alternationindex,XAML
定義的模板:
<UserControl.Resources>
<DataTemplate x:Key="ItemLeft" >
<Border Background="Blue" Height="10">
<!-- Define Left Style -->
</Border>
</DataTemplate>
<DataTemplate x:Key="ItemRight">
<Border Background="Red" Height="10">
<!-- Define Right Style -->
</Border>
</DataTemplate>
</UserControl.Resources>
我已經刪除了數據模板代碼,使其更易於閱讀。這比邊框顏色要多得多。
列表框:
<ListBox Name="StatusListBox" AlternationCount="2">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource ItemLeft}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ContentPresenter}}, Path=(ListBox.AlternationIndex)}" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource ItemRight}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
該代碼沒有設置內容控制得當。我要麼做錯了,要麼錯過了一步。我是使用WPF的新手,我發現它大部分非常直觀,但我迷失在這裏。我想嘗試將其包含到XAML代碼中。
感謝
我甚至沒有看到有關aternationindex +1部分 – 2015-02-09 18:57:04