列表框WPF项目背景颜色

本文关键字:背景 颜色 项目 WPF 列表 | 更新日期: 2023-09-27 18:03:59

我想改变ListBoxItem的背景色

经过搜索,我决定使用这个

<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>

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

列表框WPF项目背景颜色

如果你的意思是当它被选中但不活动时,试试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>

试试这个

<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>

如果你认为系统颜色键不适合你,那么你可以通过为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>

我是这样为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>

我使用MaterialDesignThemes和Caliburn Micro来制作一个可以有多个选择的列表框。这些答案都不起作用。对我有用的是ViewModel.xaml:

<ListBox Padding="2" Margin="5" Grid.Column="1" Grid.Row="4" Name="Field1Items" SelectionMode="Multiple" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="Background" Value="{Binding ListBoxItemBackground}" />
            </Style>
        </ListBox.ItemContainerStyle>
</ListBox>

然后用这个类封装我所有的项:

public class MultiSelectItem : PropertyChangedBase
{
    public MultiSelectItem (string name)
    {
        this.Name = name;
        _isSelected = false;
    }
    public string Name { get; set; }
    bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            NotifyOfPropertyChange(() => IsSelected);
            NotifyOfPropertyChange(() => ListBoxItemBackground);
        }
    }
    public string ListBoxItemBackground
    {
        get
        {
            if (IsSelected)
                return "#e0e0e0";
            return "Transparent";
        }
    }

    public override string ToString()
    {
        return Name;
    }
}

和ViewModelClass中的

public BindableCollection<MultiSelectItem> Field1Items
{
    get
    {
        return _field1Items;
    }
}