使用 .NET 3.5 SP1 的另一种项目背景





5.00/5 (2投票s)
使用 .NET 3.5 SP1 的另一种项目背景
.NET 3.5 SP1 包含许多技巧,今天我发现了一个名为 AlternationCount
的新属性,该属性适用于所有 ItemControl
控件,例如 ItemControl
、ListBox
等。
这个简单的属性可以用来根据 AlternationIndex
的当前值提供某种替代外观。因此,您可以根据它们的索引对项目进行着色或以不同的方式格式化。
这里有一个小例子,我根据它们的 AlternationIndex
值,用不同的颜色对项目进行着色。AlternationIndex
与 ItemsControl
(或派生控件)的 AlternationCount
协同工作,因此在这个例子中,ItemsControl AlternationCount
是 3
,这意味着我们可以在 AlternationIndex
上为值 1
/2
或 3
设置触发器。
这是用于一个小示例的 XAML
1: <Window x:Class="WpfApplication1.Window1"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="Window1" Height="300" Width="300">
5:
6: <Window.Resources>
7:
8: <DataTemplate x:Key="someStringItemsTemplate">
9:
10: <Border x:Name="bord" BorderBrush="Transparent"
11: CornerRadius="0"
12: HorizontalAlignment="Stretch"
13: Background="#ff656565">
14: <Label Content="{Binding}"
15: HorizontalContentAlignment="Stretch"
16: HorizontalAlignment="Stretch"
17: Margin="0"
18: Background="Transparent"/>
19:
20: </Border>
21: <DataTemplate.Triggers>
22: <Trigger Property="ItemsControl.AlternationIndex"
23: Value="1">
24: <Setter TargetName="bord"
25: Property="Background" Value="Yellow"/>
26: </Trigger>
27: <Trigger Property="ItemsControl.AlternationIndex"
28: Value="2">
29: <Setter TargetName="bord"
30: Property="Background" Value="Orange"/>
31: </Trigger>
32: </DataTemplate.Triggers>
33: </DataTemplate>
34:
35: </Window.Resources>
36:
37: <ItemsControl x:Name="someItems" AlternationCount="3"
38: VerticalAlignment="Top"
39: HorizontalAlignment="Stretch"
40: Margin="0,0,0,0"
41: ItemTemplate="{StaticResource
42: someStringItemsTemplate}">
43: <ItemsControl.ItemsPanel>
44: <ItemsPanelTemplate>
45: <StackPanel Orientation="Vertical"/>
46: </ItemsPanelTemplate>
47: </ItemsControl.ItemsPanel>
48:
49: </ItemsControl>
50: </Window>
当使用一小部分项目运行时,我们得到