Andreas Sawatzki
Andreas Sawatzki

Reputation: 157

How to set a border around listviewitem in WPF

My ListView should have following style:

My ListView:

<ListView DisplayMemberPath="Name" BorderThickness="0" >
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <!-- get rid of the highlighting -->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <ContentPresenter />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <!-- style selected item -->
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontSize" Value="20" />
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

I tried this and this, both doesn't work for me. I guess my problem is the template but I have no idea how to put a border around the selected ListViewItem.

Update

Working solution:

    <ListView DisplayMemberPath="Name" BorderThickness="0" >
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <!-- get rid of the highlighting -->
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Border x:Name="Border">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="FontSize" Value="20" />
                                    <Setter Property="FontWeight" Value="Bold" />
                                    <Setter TargetName="Border" Property="BorderBrush" Value="Black"/>
                                    <Setter TargetName="Border" Property="BorderThickness" Value="2"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

Upvotes: 4

Views: 3770

Answers (1)

user5747953
user5747953

Reputation:

Maybe this works. Add a Border around ContentPresenter an use controlTemplate Triggers

 <ControlTemplate TargetType="{x:Type ListViewItem}">
                                <Border x:Name="Border">
                                    <ContentPresenter />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="FontSize" Value="20" />
                                        <Setter Property="FontWeight" Value="Bold" />
                                        <Setter TargetName="Border" Property="BorderBrush" Value="Red"/>
                                        <Setter TargetName="Border" Property="BorderThickness" Value="1"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>

Upvotes: 3

Related Questions