【问题标题】:Designing ListViewItems (Selection Color) is not working at all设计 ListViewItems(选择颜色)根本不起作用
【发布时间】:2019-02-20 12:09:04
【问题描述】:

我想单独设计我的 ListViewItems 的选择颜色。我发现了很多操作方法

但不知何故:似乎根本没有任何效果。 在我当前的示例中,我重新使用了该链接中的示例:How to style ListView selection?

我让它轻松地为 TreeViewItems 工作,但对于 ListViewItemsListBoxItems 都不起作用。

谁能解释一下,我做错了什么? 这是我的 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>

这就是它的外观:

我还尝试了 SystemColors 的所有可用资源键。没有任何效果。这个截图是一个新的 WpfApp 的结果,我没有全局样式。

【问题讨论】:

    标签: .net wpf xaml


    【解决方案1】:

    这里有一个使用 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>
    

    代码产生以下结果:

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

    【讨论】:

    • 实际上我想避免使用模板修改项目,因为我所有的项目都是自定义视图,包括背景。但就目前而言,这是我唯一的可能。 真正让我发疯的是,它似乎在任何地方都可以与其他解决方案一起使用。下周我将把它作为解决方案。可能有人发现了问题。
    猜你喜欢
    • 2012-08-18
    • 2014-10-31
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多