设计ListViewItems(选择颜色)根本不起作用

时间:2019-02-20 12:09:04

标签: .net wpf xaml

我想单独设计ListViewItems的选择颜色。我找到了许多操作方法

但是以某种方式:似乎什么都没有。 在当前示例中,我重复使用了该链接中的示例:How to style ListView selection?

我使它易于用于 TreeViewItems ,但是对于 ListViewItems ListBoxItems 都无效。

有人可以解释一下我在做什么错吗? 这是我的xaml:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"             
    mc:Ignorable="d"
    Height="200" Width="300">
<Window.Resources>
    <Style TargetType="ListViewItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" Color="Gray"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
        </Style.Resources>
    </Style>
    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="true" >
                <Setter Property="Foreground" Value="Red" /> <!-- only foreground works -->
                <Setter Property="Background" Value="Green" />
            </Trigger>
        </Style.Triggers>
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" Color="Gray"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
        </Style.Resources>
    </Style>
    <Style TargetType="TreeViewItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrush}" Color="Gray"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Red"/>
        </Style.Resources>
    </Style>
</Window.Resources>
<StackPanel>
    <ListBox>
        <ListBoxItem>Item a 1</ListBoxItem>
        <ListBoxItem>Item a 2</ListBoxItem>
    </ListBox>
    <ListView>
        <ListViewItem>Item b 1</ListViewItem>
        <ListViewItem>Item b 2</ListViewItem>
    </ListView>
    <TreeView>
        <TreeViewItem Header="Node Level 0">
            <TreeViewItem Header="Node Level 1" />
        </TreeViewItem>
    </TreeView>
</StackPanel>
</Window>

这总是它的样子:

enter image description here

我也已经尝试了SystemColors的所有可用资源键。没有任何影响。该屏幕截图是新WpfApp的结果,我没有全局样式。

1 个答案:

答案 0 :(得分:0)

这是使用ListBox和ListView做到这一点的一种方法:

<Window.Resources>

    <Style TargetType="{x:Type ListViewItem}" >
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border" Padding="0,5,0,5" SnapsToDevicePixels="true" Background="White" BorderThickness="1">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="White"/>
                            <Setter TargetName="Border" Property="Background" Value="Red"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type ListBoxItem}" >
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border" Padding="0,5,0,5" SnapsToDevicePixels="true" Background="White" BorderThickness="1">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="White"/>
                            <Setter TargetName="Border" Property="Background" Value="Red"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<StackPanel>
    <ListBox>
        <ListBoxItem>Item a 1</ListBoxItem>
        <ListBoxItem>Item a 2</ListBoxItem>
    </ListBox>
    <ListView>
        <ListViewItem>Item b 1</ListViewItem>
        <ListViewItem>Item b 2</ListViewItem>
    </ListView>
</StackPanel>

代码产生以下结果:

Example

为此找到的最佳资源是on MSDN Forums。您可以从讨论中找到一些替代选项。