Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine index of current ListBox item from DataTemplate?

I have a ListBox. Now I want to write a DataTemplate in such way, that the first item will have red background and white background for other items. I guess I need to write a DataTrigger, but I have no idea how do determine that DataTemplate is applying to the first item.

like image 480
levanovd Avatar asked Nov 15 '09 10:11

levanovd


1 Answers

items controls have an alternation count that you use to style against

have a look here :

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="LightBlue"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="LightGreen"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

enjoy!

like image 52
Aran Mulholland Avatar answered Sep 22 '22 17:09

Aran Mulholland