【发布时间】:2015-02-10 03:16:51
【问题描述】:
我有一个带有项目控件的用户控件,我希望它有 2 种完全不同的项目模板样式,具体取决于交替索引。我看过很多关于如何根据索引更改背景颜色但不更改每个索引样式的教程。这是我目前所拥有的。
定义的模板:
<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>
我删除了数据模板代码以使其更易于阅读。它不仅仅是边框颜色。
项目控制:
<ItemsControl Name="ItemControl" AlternationCount="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Style>
<Style>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="ItemsControl.ItemTemplate" Value="{StaticResource ItemRight}"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="ItemsControl.ItemTemplate" Value="{StaticResource ItemLeft}"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
我很确定我不应该在样式中使用这种类型的触发器,但我不知道该怎么做。我是使用 WPF 的新手,我发现它大部分都很直观,但我在这里迷路了。我想尝试将其包含在 XAML 代码中。
谢谢
【问题讨论】:
-
你打算只改变背景颜色还是控件的布局也会改变?
-
我需要更改布局。 index 0 内容将左对齐, index 1 内容将右对齐。