相关疑难解决方法(0)

WPF ListView非活动选择颜色

我正在创建一个WPF应用程序,其中连续生成多个ListView选项(类似于iTunes浏览器).问题是默认的非活动选择颜色太浅.(见下文) 默认非活动选择颜色(太亮)

如何更改此颜色,以便我的非活动列表视图如下所示?(见下文) 非活动和主动选择颜色相同

使用Style类似的方法覆盖默认的SystemColor :

<Style TargetType="ListViewItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
    </Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)

wpf listview selection

65
推荐指数
4
解决办法
4万
查看次数

WPF:ListBoxItem.IsSelected的触发器不适用于Background属性

我试图改变Background属性我ListBoxItem使用触发器s中ItemContainerStyle的我ListBox,如下所示:

    <ListBox Height="100" HorizontalAlignment="Left" Margin="107,59,0,0" Name="listBox1" VerticalAlignment="Top" Width="239">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Lightblue"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="Red"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="Yellow"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.Items>
            <ListBoxItem Content="First Item"/>
            <ListBoxItem Content="SecondItem"/>
            <ListBoxItem Content="Third Item"/>
        </ListBox.Items>
    </ListBox>
Run Code Online (Sandbox Code Playgroud)

我希望未选择的项目具有浅蓝色背景,悬停项目(即当鼠标光标在它们上方时)为黄色并且所选项目为红色.

对于未选择和悬停的项目,这是按预期工作的,但所选项目仍然具有其标准背景颜色(即蓝色,如果列表框具有焦点,否则为浅灰色).

有什么我想念的吗?这种行为是否记录在某处?

谢谢你的提示!

编辑

我知道覆盖默认系统颜色的解决方案(如更改选定和未聚焦的列表框样式中所述,不会显示为灰色,非常感谢每个人都将此作为答案发布).然而,这不是我想要做的.我更感兴趣的是为什么我的解决方案不起作用.

我怀疑标准ControlTemplateListItem定义它自己的触发器,这似乎采取precendence在由风格定义的触发器(也许有人可以证实这一点,并指出我的一些资源,其中这种行为被定义).

我的解决方案是同时ControlTemplate为我定义一个ListItem:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border …
Run Code Online (Sandbox Code Playgroud)

wpf styles background

17
推荐指数
2
解决办法
2万
查看次数

设置背景颜色或WPF(4.0)ListBox - Windows 8

我试图将所选ListBoxItem的背景颜色设置为白色而不是系统颜色.我已经阅读了我在这里可以找到的内容并且已经跟随或者相信已经遵循了那里的建议(更改所选ListBox项目的背景颜色,WPF如何在列表框失去焦点时更改列表框所选项目文本颜色,更改选中和不专心的列表框样式不会变灰,以及其他).

所有似乎都通过将HighlightBrush和ControlBrush设置为所选项目的透明来解决问题.我有以下XAML并且它正确设置了字体颜色,但无论画笔设置如何,backgroound都是默认的透明蓝色.我仍然是一个WPF菜鸟,所以我必须在这里遗漏一些简单的东西.

<ListBox Width="Auto" Height="Auto" Grid.Column="0" BorderThickness="0" Background="#FFF3F3F3" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ListBox.ItemsSource>
       <x:Array Type="{x:Type sys:String}">
          <sys:String>String 1</sys:String>
          <sys:String>String 2</sys:String>
          <sys:String>String 3</sys:String>
          <sys:String>String 4</sys:String>
       </x:Array>
    </ListBox.ItemsSource>
    <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
          <Style.Resources>
             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
          </Style.Resources>
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          <Setter Property="FontSize" Value="16"/>
          <Setter Property="Foreground" Value="#999999"/>
          <Style.Triggers>
             <Trigger Property="IsSelected" Value="True" >
                <Setter Property="Background" Value="White" />
                <Setter Property="Foreground" Value="Black" />
             </Trigger>
          </Style.Triggers>
       </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
       <DataTemplate>
          <TextBlock Text="{Binding}" HorizontalAlignment="Right" Margin="0,0,8,0" …
Run Code Online (Sandbox Code Playgroud)

c# wpf windows-8

17
推荐指数
2
解决办法
8775
查看次数

标签 统计

wpf ×3

background ×1

c# ×1

listview ×1

selection ×1

styles ×1

windows-8 ×1