WPF:基于属性值为TextBox的ListBox中的文本着色

时间:2018-09-10 20:24:49

标签: c# wpf

WPF相对较新。

我有以下XAML /代码,它们将在所述类的ObservableCollection中显示ItemNames(类的属性)。我想做的是,如果IsAvailable值为false,则为文本着色另一种颜色,如果为true,则将其保留为黑色。如何设置检查并修改颜色?

数据类:

    public class PIProductionData : INotifyPropertyChanged
{
    private string itemName;
    private bool isAvailable;

    public event PropertyChangedEventHandler PropertyChanged;

    public string ItemName
    {
        get => this.itemName;
        set
        {
            this.itemName = value;
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ItemName"));
        }
    }

    public bool IsAvailable
    {
        get => this.isAvailable;
        set
        {
            this.isAvailable = value;
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsAvailable"));
        }
    }
}

集合类:

 public class PIProducts : INotifyPropertyChanged
{
    public const int RawIndex = 0;
    public const int Tier1Index = 1;
    public const int Tier2Index = 2;
    public const int Tier3Index = 3;
    public const int Tier4Index = 4;

    private List<ObservableCollection<PIProductionData>> items;

    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<PIProductionData> Raw
    {
        get => this.items[0];
        set
        {
            this.items[0] = value;
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Raw"));
        }
    }

    public ObservableCollection<PIProductionData> Tier1
    {
        get => this.items[1];
        set
        {
            this.items[1] = value;
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Tier1"));
        }
    }

XAML:

    <ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding ProductionLists.Raw} Style="{StaticResource MyListBoxStyle}"/>

<Style x:Key="MyListBoxStyle" TargetType="ListBox">
        <Setter Property="Background" Value="LightGray"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBox Text="{Binding Path=ItemName, Mode=OneWay}"  Style="{StaticResource ListBoxItemStyle}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:1)

“文本框样式”可能包含一个DataTrigger:

<Style TargetType="TextBox" x:Key="ListBoxItemStyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsAvailable}" Value="True">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

除此之外,TextBox可能应该是TextBlock,因为您不想编辑文本。

<Style TargetType="TextBlock" x:Key="ListBoxItemStyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsAvailable}" Value="True">
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
...
<DataTemplate>
    <TextBlock Text="{Binding ItemName}" Style="{StaticResource ListBoxItemStyle}"/>
</DataTemplate>