如何在WPF中设置listviewitem的边框

时间:2016-08-02 11:45:13

标签: wpf

我的ListView应具有以下样式:

  • 整个ListView(已实现)
  • 没有边框
  • 与ListViewItem(已实现)进行交互时没有突出显示
  • 围绕选中 ListViewItem(缺失)

我的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>

我尝试了thisthis,两者都不适合我。我想我的问题是模板,但我不知道如何在选定的ListViewItem周围放置边框。

更新

工作解决方案:

    <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>

1 个答案:

答案 0 :(得分:3)

也许这有效。在ContentPresenter周围添加一个边框,使用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>