WPF根据属性给ListBoxItem上色

本文关键字:ListBoxItem 上色 属性 WPF | 更新日期: 2023-09-27 18:09:05

我看过这个答案,但由于某种原因,它不适合我。我正在使用LookupEntity对象的可观察集合:

public class LookupEntity
{
    public bool IsSelected { get; set; }
    public int Id { get; set; }
    public string Code { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsInactive { get; set; }
 }

我需要的是一个不活动的项目(IsInactive=True)的背景颜色不同于其他项目。我尝试了两种方法。第一种方法是使用DataTrigger和Template。这应该可以工作,但它不是:

    <ListBox x:Name="RecordList"
    Grid.Row="2"
    MinWidth="200"
    ItemsSource="{Binding MaintenanceList, Mode=TwoWay}"
    HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
    VerticalAlignment="Stretch"
    BorderThickness="0"
    SelectedValue="{Binding SelectedItem, Mode=TwoWay}"                            
    IsEnabled="{Binding ContentVM.AddEnabled}"
    SelectionChanged="ListBox_SelectionChanged">
    <ListBox.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsInactive}" Value="True">
                    <Setter Property="Background" Value="PaleVioletRed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <DataTemplate DataType="{x:Type utility:LookupEntity}">
            <TextBlock 
        Text="{Binding Title}"
        ToolTip="{Binding Description}"
        TextWrapping="NoWrap"
        HorizontalAlignment="Left"/>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

第二种方法可以工作,但它依赖于LookupEntity对象中的Background Color属性。我不喜欢这种方法,因为它把显示责任赋予了一个对象,而这个对象不应该知道它是如何显示的。将此属性添加到LookupEntity:

    public SolidColorBrush BackgroundColor
    {
        get { return IsInactive ? Brushes.PaleVioletRed : Brushes.Transparent; }
    }

绑定DataTemplate的Background属性可以工作,但这是不可接受的。

    <ListBox.Resources>
        <DataTemplate DataType="{x:Type utility:LookupEntity}">
            <TextBlock 
        Text="{Binding Title}"
        ToolTip="{Binding Description}"
        TextWrapping="NoWrap"
        HorizontalAlignment="Left"
        Background="{Binding BackgroundColor}"/>
        </DataTemplate>
    </ListBox.Resources>

我想把责任的背景颜色在皮肤/资源,但我不能与此设置。我错过了什么吗?

WPF根据属性给ListBoxItem上色

您的LookupEntity类应该实现INotifyPropertyChanged接口。当IsInactive发生变化时,应触发PropertyChanged事件。

public class LookupEntity : INotifyPropertyChanged
{
    private bool _isInactive;
    public bool IsSelected { get; set; }
    public int Id { get; set; }
    public string Code { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsInactive
    {
        get { return _isInactive; }
        set
        {
            _isInactive = value;
            OnPropertyChanged("IsInactive");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
编辑:您可以尝试直接绑定属性,而不是使用触发器。但是,您需要使用必须实现的值转换器。
<Style TargetType="ListBoxItem" Background="{Binding IsInactive, Converter={StaticResource boolToColorConverter}}">
</Style>