开发者

How to determine index of current ListBox item from DataTemplate?

开发者 https://www.devze.com 2022-12-11 17:19 出处:网络
I have a ListBox. Now I want to write a DataTemplate in such way, that the first item will have red backgroun开发者_高级运维d and white background for other items. I guess I need to write a DataTrigge

I have a ListBox. Now I want to write a DataTemplate in such way, that the first item will have red backgroun开发者_高级运维d 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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号