- <ItemsControl ItemsSource="{StaticResource datas}" AlternationCount="2">
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Grid Background="Transparent" Name="root">
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- <ColumnDefinition/>
- Grid.ColumnDefinitions>
- <TextBlock Text="{Binding Name}"/>
- <TextBlock Text="{Binding Age}" Grid.Column="1"/>
- Grid>
- <DataTemplate.Triggers>
- <Trigger Property="ItemsControl.AlternationIndex" Value="1">
- <Setter Property="Background" Value="Orange" TargetName="root"/>
- Trigger>
- DataTemplate.Triggers>
- DataTemplate>
- ItemsControl.ItemTemplate>
- ItemsControl>
listview无法直接通过DataTemplate.Triggers去设置,只能通过ItemContainerStyle的方式
- <ListView ItemsSource="{StaticResource datas}" AlternationCount="2">
- <ListView.ItemContainerStyle>
- <Style TargetType="ListViewItem">
- <Style.Triggers>
- <Trigger Property="ItemsControl.AlternationIndex" Value="1">
- <Setter Property="Background" Value="Orange"/>
- Trigger>
- <Trigger Property="IsMouseOver" Value="True">
-
- Trigger>
- Style.Triggers>
- Style>
- ListView.ItemContainerStyle>
- <ListView.ItemTemplate>
- <DataTemplate>
- <Grid Background="Transparent" Name="root">
- <Grid.ColumnDefinitions>
- <ColumnDefinition/>
- <ColumnDefinition/>
- <ColumnDefinition/>
- Grid.ColumnDefinitions>
- <TextBlock Text="{Binding Name}"/>
- <TextBlock Text="{Binding Age}" Grid.Column="1"/>
- Grid>
- DataTemplate>
- ListView.ItemTemplate>
- ListView>
通过snoop观察,listview比ItemsControl 多封装了几层:
由于多了个ListViewItem这一次,所以没法直接访问到ItemsControl.AlternationIndex
所以用了ItemContainerStyle的方式去访问。