C# 列表框 WPF 项目背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15431097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:48:33  来源:igfitidea点击:

Listbox WPF item background color

c#wpfxaml

提问by takayoshi

I want to change the background color of ListBoxItem

我想改变 ListBoxItem 的背景颜色

After search I decide to use this

搜索后我决定使用这个

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

        </ListBox.Resources>
        <TextBlock>fdfsdf1</TextBlock>
        <TextBlock>fdfsdf3</TextBlock>
        <TextBlock>fdfsdf5</TextBlock>
        <TextBlock>fdfsdf3</TextBlock>
        <TextBlock>fdfsdf4</TextBlock>
    </ListBox>

When a listboxitem is focused, the background is blue as expected, but when the selected listboxitem loses focus, the background turns gray. How can I make the background remain blue when it loses focus?

当一个 listboxitem 被聚焦时,背景是预期的蓝色,但是当被选中的 listboxitem 失去焦点时,背景会变成灰色。如何在失去焦点时使背景保持蓝色?

采纳答案by sa_ddam213

if you mean just when its selected but inactive try InactiveSelectionHighlightBrushKey

如果你的意思是当它被选中但不活动时尝试 InactiveSelectionHighlightBrushKey

    <ListBox.Resources>
        <!-- Background of selected item when focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                         Color="Blue" />
        <!-- Background of selected item when not focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                         Color="Blue" />

    </ListBox.Resources>

回答by failedprogramming

Try this

尝试这个

<ListBox>
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Background" Value="Blue" />
            </Style>
        </ListBox.Resources>
        <TextBlock>fdfsdf1</TextBlock>
        <TextBlock>fdfsdf3</TextBlock>
        <TextBlock>fdfsdf5</TextBlock>
        <TextBlock>fdfsdf3</TextBlock>
        <TextBlock>fdfsdf4</TextBlock>
    </ListBox>

回答by JSJ

if you think the system color keys are not working for you then you can force it by creating new style for ListboxItems as like below.

如果您认为系统颜色键对您不起作用,那么您可以通过为 ListboxItems 创建新样式来强制它,如下所示。

        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Silver"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

回答by Will

This is what I used to select the color for the active/inactive items of a ListBox:

这是我用来为 ListBox 的活动/非活动项目选择颜色的方法:

<ListBox Name="lbExample" SelectionMode="Multiple">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <...>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Resources>
            <!-- Background of selected item when not focussed -->
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="AntiqueWhite" />
            </Style>

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