XAML 列表框WPF项背景色

idfiyjo8  于 2022-12-07  发布在  其他
关注(0)|答案(5)|浏览(182)

我要变更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失去焦点时,背景变成灰色。我如何使背景在失去焦点时保持蓝色?

siv3szwd

siv3szwd1#

如果您的意思是只是当它被选中但不活动尝试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>
qxsslcnc

qxsslcnc2#

试试这个

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

uxh89sit3#

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

vngu2lb84#

这是我用来选择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>
ftf50wuq

ftf50wuq5#

我正在使用MaterialDesignThemes和Caliburn Micro来创建一个可以有多个选择的ListBox。这些其他的答案都不起作用。对我起作用的是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;
    }
}

相关问题