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>

 

posted @ 2020-05-23 10:47  _York  阅读(1140)  评论(0编辑  收藏  举报