WPF listBox选中项改变样式

Posted tangchun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF listBox选中项改变样式相关的知识,希望对你有一定的参考价值。

<UserControl x:Class="Mjj.MyUserControls.LeftMenu"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
    <UserControl.Resources>

        <Style x:Key="LeftMenuListBoxStyle" TargetType="ListBox">
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                    <Border x:Name="Border" Height="46px" BorderThickness="0 0 0 1" BorderBrush="#E4E7ED"  VerticalAlignment="Center">
                                        <Label x:Name="Label" VerticalAlignment="Center" HorizontalAlignment="Center"  FontSize="14px" Foreground="#202122" >
                                            <ContentPresenter></ContentPresenter>
                                        </Label>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsSelected" Value="True">
                                            <Setter TargetName="Border" Property="BorderBrush" Value="#0089FF"></Setter>
                                            <Setter TargetName="Border" Property="Background" Value="#D9E0EE"></Setter>
                                            <Setter TargetName="Label" Property="Foreground" Value="#F2901C" ></Setter>
                                            <Setter TargetName="Border" Property="BorderThickness" Value="0 0 1 0"></Setter>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>

                       
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <ListBox Style="{StaticResource LeftMenuListBoxStyle}" DisplayMemberPath="MenuName"  BorderThickness="0" Width="160px" Name="ListBoxLeftMenu"  >

    </ListBox>
</UserControl>

 

以上是关于WPF listBox选中项改变样式的主要内容,如果未能解决你的问题,请参考以下文章

wpf listbox 选中项 上移下移

加载 WPF 时在 ListBox 中设置选定项的样式

用wpf或者blend怎么修改listbox被选中项的颜色

wpf中如何绑定到listbox中的选定项

WPF中的数据绑定

WPF中的数据绑定