Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF基于属性为ListBoxItem着色_C#_Wpf - Fatal编程技术网

C# WPF基于属性为ListBoxItem着色

C# WPF基于属性为ListBoxItem着色,c#,wpf,C#,Wpf,我已经看过了,但出于某种原因,它对我不起作用。我使用的是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 Descri

我已经看过了,但出于某种原因,它对我不起作用。我使用的是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和模板。这应该是可行的,但不是:

    <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>
绑定DataTemplate的后台属性是可行的,但这是不可接受的

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


我想将背景颜色的责任放在皮肤/资源中,但我不能使用此设置。我错过了什么吗

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

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

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

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



我刚刚试用了您的代码,如果IsInactive已设置,它对我有效。我猜您稍后会更新IsInactive,因为您没有发出属性更改通知,所以触发器永远不会知道要触发,因为您需要实现INotifyPropertyChanged InterfaceInactive不更改,它是预设的。除了我,其他人都能用?精彩的。我讨厌这些类型的问题。我刚刚试用了你的代码,如果已经设置了IsInActivity,它对我来说是有效的。我猜您稍后会更新IsInactive,因为您没有发出属性更改通知,所以触发器永远不会知道要触发,因为您需要实现INotifyPropertyChanged InterfaceInactive不更改,它是预设的。除了我,其他人都能用?精彩的。我讨厌这些类型的问题。我刚刚试用了你的代码,如果已经设置了IsInActivity,它对我来说是有效的。我猜您稍后会更新IsInactive,因为您没有发出属性更改通知,所以触发器永远不会知道要触发,因为您需要实现INotifyPropertyChanged InterfaceInactive不更改,它是预设的。除了我,其他人都能用?精彩的。我讨厌这些类型的问题。我刚刚试用了你的代码,如果已经设置了IsInActivity,它对我来说是有效的。我猜您稍后会更新IsInactive,因为您没有发出属性更改通知,所以触发器永远不会知道要触发,因为您需要实现INotifyPropertyChanged InterfaceInactive不更改,它是预设的。除了我,其他人都能用?精彩的。我讨厌这些类型的问题。问题是财产永远不会改变。我试过了,但没用。将生成每个LookupEntity并将其添加到列表中。完成后,列表不会更改。不需要更改InotifyProperty。DataTrigger是否仅在数据更改时才起作用?如果是这样的话,还有别的方法吗?太好了!您使用转换器绑定到属性的建议成功了。非常感谢!问题是,属性永远不会改变。我试过了,但没用。将生成每个LookupEntity并将其添加到列表中。完成后,列表不会更改。不需要更改InotifyProperty。DataTrigger是否仅在数据更改时才起作用?如果是这样的话,还有别的方法吗?太好了!您使用转换器绑定到属性的建议成功了。非常感谢!问题是,属性永远不会改变。我试过了,但没用。将生成每个LookupEntity并将其添加到列表中。完成后,列表不会更改。不需要更改InotifyProperty。DataTrigger是否仅在数据更改时才起作用?如果是这样的话,还有别的方法吗?太好了!您使用转换器绑定到属性的建议成功了。非常感谢!问题是,属性永远不会改变。我试过了,但没用。将生成每个LookupEntity并将其添加到列表中。完成后,列表不会更改。不需要更改InotifyProperty。DataTrigger是否仅在数据更改时才起作用?如果是这样的话,还有别的方法吗?太好了!您使用转换器绑定到属性的建议成功了。非常感谢!