ListBox 中的数据行实现颜色交替
WPF内置的两大专用属性ItemsControl.AlternationCount 属性和 ItemsControl.AlternationIndex 附加属性来完成交替条目的样式改变。
ItemsControl.AlternationCount :ItemsControl的交替项容器数。
ItemsControl.AlternationIndex: 条目项所在项容器中时被赋予的用于表示交替项位置的索引标志。
ListBox中设置
<ListBox AlternationCount="2" >
<ListBox.ItemContainerStyle>主要是通过设置触发器<Style.Triggers>,<Trigger Property="ItemsControl.AlternationIndex" Value="1"> 实现颜色交替显示
<Window x:Class="ZQWin007B.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ZQWin007B"
mc:Ignorable="d"
x:Name="mainWin"
Title="Window1" Height="450" Width="800">
<ListBox x:Name="YearList" DockPanel.Dock="Bottom" Margin="3" ItemsSource="{Binding DataYears}"
AlternationCount="2" >
<!--样式设置-->
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ListBoxItem.Background" Value="LightSteelBlue"/>
<Setter Property="Margin" Value="3"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Style.Triggers>
<!--触发器,通过设置 AlternationCount="2"ItemsControl.AlternationIndex 实现颜色交替显示 -->
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="ListBoxItem.IsSelected" Value="True">
<Setter Property="Background" Value="DarkBlue" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<!--鼠标左键双击-->
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Text}" ToolTip="鼠标左键双击,采集本年度!">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.SelectListBoxItemCommand, ElementName=mainWin }"
CommandParameter="{Binding ElementName=YearList}"
MouseAction="LeftDoubleClick"></MouseBinding>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>