Jonatan Dragon
Jonatan Dragon

Reputation: 5017

First item custom style border in ListView

How can I set different style for first item in ListView? In my case, I want to change first item border, to get GUI like this:

list with different border for first item

My current code (no top border):

<ListView 
    ItemsSource="{Binding MyData}">
    <ListView.ItemContainerStyle>
        <Setter Property="BorderThickness" Value="0,0,0,1" />
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 1

Views: 1108

Answers (1)

Jonatan Dragon
Jonatan Dragon

Reputation: 5017

There is a very simple solution. You don't have to write custom converters etc. Use PreviousData in RelativeSource

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="BorderThickness" Value="0,0,0,1" />
        <Style.Triggers>
            <DataTrigger 
                Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                <Setter Property="BorderThickness" Value="0,1,0,1"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

Upvotes: 3

Related Questions