WPF Using Alternating Rows in an ItemsControl

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            List<Employee> emps = new List<Employee>();
            emps.Add(new Employee() {FirstName="John", LastName="Doe"});
            emps.Add(new Employee() { FirstName = "Bruce", LastName = "Wayne" });
            emps.Add(new Employee() { FirstName = "Dick", LastName = "Grayson" });
            emps.Add(new Employee() { FirstName = "James", LastName = "Kirk" });
            emps.Add(new Employee() { FirstName = "Blue", LastName = "Falcon" });
            emps.Add(new Employee() { FirstName = "Babe", LastName = "Ruth" });
            lstNames.ItemsSource=emps;
        }
    }
    public class Employee
    {
        public string FirstName {get;set;}
        public string LastName {get;set;}
    }

 

<Window x:Class="HDI_WPF_Alternation_cs.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="LightBlue" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
        <Grid>
        <ListView x:Name="lstNames" AlternationCount="3">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path=FirstName}" />
                        <TextBlock Text="{Binding Path=LastName}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

posted @ 2009-11-27 15:33  pursue  阅读(101)  评论(0)    收藏  举报