【问题标题】:Set background color for selected items in a ListBox为 ListBox 中的选定项目设置背景颜色
【发布时间】:2011-10-26 22:26:30
【问题描述】:

我无法为列表框中的选定项目设置背景颜色。我不想要这个例子中的交替颜色。我把它们作为测试,它们工作。当字体粗体变为粗体且前景变为红色时,触发器 IsSelected 正在触发。将高亮颜色笔刷设置为 SteelBlue 不会达到预期的效果,因为它会在 ListBox 失去焦点时消失。当 ListBox 失去焦点并且是我想要的时,红色和粗体确实保持不变。我希望为所选项目采用并保留背景颜色。现在,所选项目的背景是白色的,并且在 ListBox 失去焦点时保持不变。感谢您的帮助,我会测试任何建议的修复方法。

    <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" Visibility="Visible" BorderThickness="2" Margin="1" Padding="2,2,7,2"
             ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" HorizontalAlignment="Left" 
             PresentationTraceSources.TraceLevel="High" AlternationCount="2" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="LightGreen"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="LightPink"></Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="Background" Value="SteelBlue" />
                        <Setter Property="Foreground" Value="Red" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Background="Transparent" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

    标签: wpf xaml listbox background selecteditem


    【解决方案1】:

    您使用 SystemColors.HighlightBrushKey(聚焦)和 SystemColors.ControlBrushKey(未聚焦)为 ListBox 指定 SelectedItem 背景

    <Style.Resources>
        <!-- Background of selected item when focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                         Color="Green"/>
        <!-- Background of selected item when not focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                         Color="LightGreen" />
    </Style.Resources> 
    

    【讨论】:

    • 值得一提的是,截至 2015 年 1 月,这对具有标准主题的 System.Windows.Controls.ListBox 的选择突出显示颜色没有影响。触发器适用于前景但不适用于背景。您必须重新模板 ListBoxItem。
    • 截至 2020 年的工作(但混乱)解决方案:stackoverflow.com/questions/64361138/…
    【解决方案2】:
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">SteelBlue</SolidColorBrush>
    </ListBox.Resources>
    

    如果你希望它也应用失焦,你需要覆盖一个额外的键:

    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}">SteelBlue</SolidColorBrush>
    

    【讨论】:

    • 我不得不接受另一个作为语法更简洁的语法,但我给了你一个 +1 谢谢
    • @BalamBalam:“干净”是值得商榷的,我的回答快了七分钟,哦,好吧,无论如何谢谢...
    • @H.B.抱歉,实际上并没有注意到您在我之前回答了几乎相同的问题。无论如何,这里是 +1
    • @Meleak:真的不用担心,发生了,事实上,如果你记得的话,我们之前在其他地方也遇到过类似的情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2011-08-04
    • 2011-08-28
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多