Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i'm new to wpf..i'm using a listbox inside inside another.. the items inside the two listbox should appear one below the other with alternate colors.. i could successfully set alternate row colors for items of individual listboxes.. but how do i do it when i'm using the listboxes one inside another? i'm using datatemplates.

because i'm using one listbox another the colors of the items look somewhat like this:

gray ->listbox 1
yellow ->listbox 1
gray -> listbox 1
gray ->listbox 2 (this color should be ideally yellow)
yellow ->listbox 2 (this color should be gray)
Posted

1 solution

Set the AlternationCount property of the ListView and use a trigger or bind to ItemsControl.AlternationIndex in order to set the background color.

Edit sorry miss read, I've changed the xaml below so the first item in the inner ListView is yellow.

XML
<UserControl x:Key="test">
    <UserControl.Resources>
        <Style x:Key="itemStyle" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="Grey" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Yellow" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style x:Key="itemStyle2" TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="Yellow" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="Grey" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <ListView AlternationCount="2" ItemContainerStyle="{StaticResource itemStyle}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ListView AlternationCount="2" ItemContainerStyle="{StaticResource itemStyle2}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</UserControl>
 
Share this answer
 
v4
Comments
Simon Bang Terkildsen 26-Aug-11 5:35am    
I see someone didn't like my answer, what's wrong with it? if you would be so kind to tell me whats wrong with it
tomgeo19 2-Sep-11 11:00am    
sorry .. my bad

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900