Wpf 更改所选列表框项目的背景色

Wpf 更改所选列表框项目的背景色,wpf,styles,Wpf,Styles,到目前为止,这是我的XAML 日期: 严重程度: 唯一的问题是所选项目的右侧有一个蓝色框。我假设有一种方法可以更改选择颜色,但我找不到它。 <UserControl.Resources> <Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}"> <Style.Resources> <SolidColorBrush x:Key="{x:Stat

到目前为止,这是我的XAML


日期:
严重程度:
唯一的问题是所选项目的右侧有一个蓝色框。我假设有一种方法可以更改选择颜色,但我找不到它。


<UserControl.Resources>
    <Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                             Color="Transparent"/>
        </Style.Resources>
    </Style>
</UserControl.Resources> 


您只需覆盖listboxitem的样式(请参阅:TargetType是listboxitem)

您需要使用的样式

ListBox.ItemTemplate指定项目内容的显示方式。但是WPF仍然将每个项目包装在ListBoxItem控件中,默认情况下,如果选中该控件,则会将其背景设置为系统高亮显示颜色。您不能停止WPF创建ListBoxItem控件,但您可以设置它们的样式——在您的例子中,将背景设置为始终透明或黑色或其他颜色——为此,您可以使用ItemContainerStyle


显示了一种可能的实现,通过在项目样式的上下文中“劫持”系统背景笔刷资源;另一种可能更为惯用的方法是对Background属性使用
Setter

或者您可以直接将HighlightBrushKey应用于列表框。Setter Property=“Background”Value=“Transparent”不起作用。但我必须将前景设置为黑色

<ListBox  ... >
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True" >
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="Foreground" Value="Black" />
                </Trigger>
            </Style.Triggers>
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
            </Style.Resources>
        </Style>                
    </ListBox.ItemContainerStyle>
</ListBox>

我必须同时设置HighlightBrushKey和ControlBrushKey,才能正确设置样式。否则,当它具有焦点时,将正确使用透明的HighlightBrusKey。Bt,如果控件失去焦点(仍高亮显示),则使用ControlBrushKey

<Style.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</Style.Resources>

希望这能帮助一些人。

如果选择不重要,最好使用ScrollViewer中包装的ItemsControl。这种组合比Listbox(实际上已经从ItemsControl派生)更轻,使用它将消除使用廉价的hack来覆盖ItemsControl中已经缺少的行为的需要

如果选择行为实际上很重要,那么这显然不起作用。但是,如果您希望更改所选项目背景的颜色,使其对用户不可见,那么这只会使他们感到困惑。如果您打算更改某些其他特征以表明该项目已被选中,则此问题的某些其他答案可能仍然更相关

以下是标记的外观框架:

    <ScrollViewer>
        <ItemsControl>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    ...
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>

...

您必须像这样为项目选择创建一个新模板

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <Border
                BorderThickness="{TemplateBinding Border.BorderThickness}"
                Padding="{TemplateBinding Control.Padding}"
                BorderBrush="{TemplateBinding Border.BorderBrush}"
                Background="{TemplateBinding Panel.Background}"
                SnapsToDevicePixels="True">
                <ContentPresenter
                    Content="{TemplateBinding ContentControl.Content}"
                    ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

我尝试过各种解决方案,但没有一种对我有效,经过进一步研究,我在这里找到了一种对我有效的解决方案


你好
你好

我在这里发布了它,因为这是针对这个问题的第一个谷歌结果,所以其他人可能会觉得它很有用。

好的,现在它更有意义了。谢谢。如果您只对“IsSelected”中的“Background”属性使用“Setter”是无效的,在ItemContainerStyle中也是如此。它仍然使用系统高亮显示颜色:(要更改所选ListBoxItem的背景色,您需要重新模板ListBoxItem。参考:在.VS 2012、.Net Framework 4.5中接受的答案中的注释。您能否提供一个基于ListBox.ItemContainerStyle的解决方案的工作示例?它似乎不工作,这是唯一的工作解决方案(不基于系统颜色)正在重新模板。这对我帮助很大。我不知道列表框未聚焦时使用的SystemColor笔刷是什么:)+1这对于我在右键单击时使背景透明非常重要。谢谢使用InactiveSelectionHighlightBrushKey代替.NET 4.5中的ControlBrushKey。这不再适用于Windows-8,它在
ControlTemplate
触发器中使用静态颜色。您必须派生基本
样式
,并在这些触发器中指定过度使用的笔刷,或者直接指定颜色@Viv这是否也适用于.net 4.5中的wpf?@Gusdor是的,Windows-7上的.net4.5使用
控制模板
使用
系统颜色
表示状态。然而,在Windows-8中,它不再像前面解释的那样工作。每个操作系统版本的差异似乎比每个.net版本的差异更大一个好的答案不仅仅是一个可行的答案,OP应该理解他的错误,而不仅仅是复制粘贴它。否则他/她会在一周后回来,在不同的上下文中问相同的问题……这个解决方案对我有效,但@juFo的答案无效,因为所有控件都给transparnt,看不到任何文本。
    <ScrollViewer>
        <ItemsControl>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    ...
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="ListBoxItem">
            <Border
                BorderThickness="{TemplateBinding Border.BorderThickness}"
                Padding="{TemplateBinding Control.Padding}"
                BorderBrush="{TemplateBinding Border.BorderBrush}"
                Background="{TemplateBinding Panel.Background}"
                SnapsToDevicePixels="True">
                <ContentPresenter
                    Content="{TemplateBinding ContentControl.Content}"
                    ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                    HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                    VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                    SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
   <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border"
                                Padding="2"
                                SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
                 Width="200" Height="250"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 ScrollViewer.HorizontalScrollBarVisibility="Auto">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>Hi</ListBoxItem>
        </ListBox>