Относительно новый для WPF.
У меня есть следующий XAML / код, который будет отображать ItemNames (свойство класса) в ObservableCollection указанного класса. Я хочу закрасить текст другим цветом, ЕСЛИ значение IsAvailable равно false, и оставить его черным, если оно истинно. Как настроить проверку и изменить цвет?
Класс данных:
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>