WPF listview选择的样式

时间:2017-06-01 09:52:59

标签: c# wpf listview

你能帮我吗,默认情况下wpf listview对所选项目有这样的风格 enter image description here

但我需要像选择这样的项目一样使它像背景一样

enter image description here

我怎么能做到这一点?

2 个答案:

答案 0 :(得分:1)

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

在Windows 8及更高版本中,您应该覆盖ListViewItem的控件模板,如下所述:

ListView Selected Item Style Override

答案 1 :(得分:1)

您可以通过ControlTemplate解决此问题。见这里: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>