更改所选ListBox项目的背景颜色

Jon*_*len 60 wpf styles

到目前为止,这是我的XAML.

    <ScrollViewer Grid.Column="1" Grid.RowSpan="2">

        <ListBox   Background="Black" ItemsSource="{Binding Path=ActiveLog}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="Black">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Foreground="White">
                            <TextBlock >Date:</TextBlock>
                            <TextBlock  Text="{Binding Path=LogDate}"/>
                        </TextBlock>
                        <TextBlock Grid.Column="1" Grid.Row="0" Foreground="White">
                            <TextBlock >Severity:</TextBlock>
                            <TextBlock  Text="{Binding Path=Severity}"/>
                        </TextBlock>
                        <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.Template>
                <ControlTemplate>
                    <StackPanel Background="Black" IsItemsHost="True" >
                    </StackPanel>
                </ControlTemplate>
            </ListBox.Template>

        </ListBox>
    </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)

唯一的问题是所选项目右侧有一个蓝色框.我假设有一种方法可以改变选择颜色,但我找不到它.

juF*_*uFo 73

<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> 
Run Code Online (Sandbox Code Playgroud)

<ListBox ItemsSource="{Binding Path=FirstNames}"
         ItemContainerStyle="{StaticResource myLBStyle}">  
Run Code Online (Sandbox Code Playgroud)

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

  • 这不再适用于在`ControlTemplate`触发器中使用静态颜色的Windows-8.您必须派生基础`Style`并在这些触发器中指定过度使用的画笔或直接给出颜色.http://stackoverflow.com/a/16820062/1834662 (18认同)

pap*_*zzo 52

或者,您可以将HighlightBrushKey直接应用于ListBox.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>
Run Code Online (Sandbox Code Playgroud)


ito*_*son 47

您需要使用ListBox.ItemContainerStyle.

ListBox.ItemTemplate指定应如何显示项目的内容.但WPF仍会将每个项目包装在ListBoxItem控件中,默认情况下,如果选中它,则将其Background设置为系统突出显示颜色.你无法阻止WPF创建ListBoxItem控件,但你可以设置样式 - 在你的情况下,将背景设置为始终是透明或黑色或其他 - 并且为此,你使用ItemContainerStyle.

juFo的答案通过在项目样式的上下文中"劫持"系统背景画笔资源来显示一种可能的实现方式; 另一种,也许更惯用的技术是使用一个Setter用于Background属性.

  • 如果您只是在 ItemContainerStyle 中为“IsSelected”中的“Background”属性使用“Setter”,则它不起作用。它仍然使用系统高亮颜色。:( (2认同)
  • 您能否提供基于 ListBox.ItemContainerStyle 的解决方案的工作示例?看起来它不起作用,唯一有效的解决方案(不基于系统颜色)是重新模板。 (2认同)

Ric*_*chS 30

我必须设置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>
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人.

  • 使用InactiveSelectionHighlightBrushKey而不是.NET 4.5中的ControlBrushKey. (17认同)

use*_*241 8

如果选择不重要,最好使用ScrollViewer中包含的ItemsControl.这个组合比Listbox(实际上已经从ItemsControl派生)更轻量级,并且使用它将消除使用廉价的hack来覆盖ItemsControl中已经不存在的行为的需要.

如果选择行为实际上很重要,那么这显然是行不通的.但是,如果要更改"所选项目背景"的颜色,使其对用户不可见,那么这只会使它们混淆.如果您打算更改某些其他特征以指示该项目已被选中,则此问题的其他一些答案可能仍然更具相关性.

以下是标记应如何显示的框架:

    <ScrollViewer>
        <ItemsControl>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    ...
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
Run Code Online (Sandbox Code Playgroud)


小智 8

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 一个好的答案不仅仅是一个有效的答案,OP应该理解他的错误,而不仅仅是复制粘贴它.否则他/她将在一周后回来在不同的背景下问同样的问题...... (8认同)

Ciu*_*caS 7

我尝试了各种解决方案,但没有一个对我有用,经过更多研究后,我在这里找到了一个对我有用的解决方案

https://gist.github.com/LGM-AdrianHum/c8cb125bc493c1ccac99b4098c7eeb60

   <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>
Run Code Online (Sandbox Code Playgroud)

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