简体   繁体   English

WPF基于属性为ListBoxItem着色

[英]WPF Color a ListBoxItem based on a property

I have looked at this answer , but it is not working for me for some reason. 我已经看过这个答案 ,但是由于某种原因,它对我不起作用。 I am using an Observable Collection of LookupEntity objects: 我正在使用一个LookupEntity对象的Observable集合:

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

What I need is for the background color of an inactive item (IsInactive=True) to be different than the other items. 我需要的是非活动项目的背景颜色(IsInactive = True)与其他项目不同。 I have tried two approaches. 我尝试了两种方法。 The first is to use a DataTrigger and a Template. 第一种是使用DataTrigger和Template。 This should work, but it is not: 这应该可以,但是不能:

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

The second approach works, but it depends on a Background Color property in the LookupEntity object. 第二种方法有效,但是它取决于LookupEntity对象中的Background Color属性。 I don't like this approach because it gives display responsibility to an object that shouldn't have any knowledge of how it's displayed. 我不喜欢这种方法,因为它给不应该如何显示的对象赋予显示责任。 Adding this property to the LookupEntity: 将此属性添加到LookupEntity:

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

And binding the Background property of the DataTemplate works, but it's not acceptable. 并绑定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>

I'd like to place the responsibility for the Background color in a skin / resource, but I can't with this setup. 我想将“背景”颜色的责任放在皮肤/资源中,但是我无法使用此设置。 Is there something I've missed? 有什么我想念的吗?

Your LookupEntity class should implement INotifyPropertyChanged interface. 您的LookupEntity类应实现INotifyPropertyChanged接口。 And you should raise the PropertyChanged event when IsInactive changes. 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));
    }
}

EDIT: You can try binding the property directly instead of using a trigger. 编辑:您可以尝试直接绑定属性,而不使用触发器。 However, you need to use a value converter, which you have to implement. 但是,您需要使用必须实现的值转换器。

<Style TargetType="ListBoxItem" Background="{Binding IsInactive, Converter={StaticResource boolToColorConverter}}">
</Style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以将 WPF ListBoxItem 的背景设置为存储为对象属性内的字符串的十六进制颜色? - Is it possible to set a WPF ListBoxItem's background to a hex color stored as a string inside an objects property? WPF:以编程方式更改 MouseOver 的 ListBoxItem 背景属性 - WPF: Change ListBoxItem Background property for MouseOver programmatically 如何获取WPF ListBoxItem属性的值 - How to get value of a property of the WPF ListBoxItem 根据ViewModel的属性更改ListBoxItem颜色 - Changing ListBoxItem colour based on Property on ViewModel 如何将 WPF ListBoxItem 的 IsEnabled 属性绑定到其源项的属性? - How to bind the IsEnabled Property of a WPF ListBoxItem to a Property of its Source Item? 根据具体对象的Color属性设置WPF DataGridRow背景颜色 - Set WPF DataGridRow Background Color Based on a Color property of a concrete object 为ListBoxItem绑定mouseLeftButtonDown和PropertyChanged(mvvm,wpf)上的背景色 - Bind mouseLeftButtonDown for ListBoxItem and background color on PropertyChanged (mvvm, wpf) C#WPF-数据触发器可根据条件更改listboxitem中的背景颜色 - C# WPF - Data trigger to change background color in listboxitem on condition 在WPF中的ListBoxItem上触发触发器时,如何设置父元素的属性 - How to set a property of the parent element, when firing a trigger on ListBoxItem in WPF WPF:基于属性值为TextBox的ListBox中的文本着色 - WPF: Color text in ListBox of TextBox based on property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM