简体   繁体   English

WPF listview选择的样式

[英]WPF listview selecteditem style

can you help me, by default wpf listview have such style for selected item 你可以帮我吗,默认情况下wpf listview对所选项目有这样的风格 在此输入图像描述

But i need to make it like solid background on selected item like this 但我需要像这样选择项目的固体背景

在此输入图像描述

How i can make this? 我怎么能做到这一点?

On Windows 7 you could override the system color brushes: 在Windows 7上,您可以覆盖系统颜色画笔:

<ListView>
    <ListView.Resources>
        <Style TargetType="ListViewItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
            </Style.Resources>
        </Style>
    </ListView.Resources>
    ...
</ListView>

On Windows 8 and later you should override the control template of the ListViewItem as explained here: 在Windows 8及更高版本中,您应该覆盖ListViewItem的控件模板,如下所述:

ListView Selected Item Style Override ListView所选项目样式覆盖

You can solve this via ControlTemplate . 您可以通过ControlTemplate解决此问题。 See here: https://blog.jsinh.in/change-background-color-of-selected-listboxitem-listbox-in-wpf/ 见这里: https//blog.jsinh.in/change-background-color-of-selected-listboxitem-listbox-in-wpf/

<Style TargetType="{x:Type 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>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="Selector.IsSelectionActive"
                             Value="False" />                          
                        <Condition Property="IsSelected"
                             Value="True" />
                     </MultiTrigger.Conditions>
                     <Setter Property="Background"
                             TargetName="Bd"
                             Value="DarkOrange" />
                </MultiTrigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="Selector.IsSelectionActive"
                                   Value="True" />
                        <Condition Property="IsSelected"
                                   Value="True" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background"
                            TargetName="Bd"
                            Value="OrangeRed" />
                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM