列表框样式不起作用

Mat*_*ann 1 wpf xaml

我试图让我的所选项目的突出显示颜色ListBoxListBox失去焦点时保持不变。经过数小时搜索互联网并尝试不同的解决方案后,我无法获得任何工作。请帮助我理解为什么我现在尝试的解决方案似乎没有改变任何东西。

这是在 Window 中我的 xaml 顶部定义的:

<Window.Resources>
    <Style x:Key="myListboxStyle">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
        </Style.Resources>
    </Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

这是我的列表框:

<ListBox x:Name="lstNaes" Style="{StaticResource myListboxStyle}"  DisplayMemberPath="Name" Margin="5" SelectionMode="Extended"/>
Run Code Online (Sandbox Code Playgroud)

颜色(红色)只是为了测试。我真正想要的是默认的高亮颜色,并且当ListBox(或ListView)失去焦点时它不会改变。我不明白为什么我找到的解决方案似乎对我不起作用。

mm8*_*mm8 5

如果您使用的是 Windows 8 或更高版本,则应ControlTemplateListViewItem容器定义自定义:

<ListBox x:Name="lstNaes" DisplayMemberPath="Name" Margin="5" SelectionMode="Extended">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" TargetName="Bd" Value="#1F26A0DA"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#a826A0Da"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" TargetName="Bd" Value="#3D26A0DA"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)