I'm getting the status of multiple items from variables in my code-behind and I'd like to display the status for these items graphically on every tab in my GUI. I'm displaying the status as a color-coded label (3 different colors represent 3 different states). I have figured out how to force the background to change colors with a bunch of code but I think using data triggers would be better however I've been unable to get it to work. Using the debugger, it steps through the routines and change the values correctly, but the colors on the screen never change. By the way, I've only been working with wpf and C# for about 6 weeks. Thank you in advance for pointing out my mistakes.
Here's my class:
class componentStatus : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private int _icc1status;
public int Icc1status
{
get
{
return this._icc1status;
}
set
{
if (value != this._icc1status)
{
this._icc1status = value;
NotifyPropertyChanged("Icc1status");
}
}
}
private int _icc2status;
public int Icc2status
{
get
{
return this._icc2status;
}
set
{
if (value != this._icc2status)
{
this._icc2status = value;
NotifyPropertyChanged("Icc2status");
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Here's the xaml:
<Style x:Key="Icc2Status" TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding Icc2status, UpdateSourceTrigger=PropertyChanged}" Value="2">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icc2status, UpdateSourceTrigger=PropertyChanged}" Value="1">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icc2status, UpdateSourceTrigger=PropertyChanged}" Value="0">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Label x:Name="icc2statusLabel" Style="{DynamicResource Icc2Status}" Content="ICC 2" HorizontalContentAlignment="Center" HorizontalAlignment="Right" Margin="0,0,260,0" VerticalAlignment="Top" Width="45" Foreground="Black" BorderThickness="1" BorderBrush="Black"/>
And my code behind:
public partial class MainWindow : Window
{
componentStatus compStatus = new componentStatus();
public static readonly DependencyProperty myComponentProperty =
DependencyProperty.Register("Icc2status", typeof(int), typeof(Label), new PropertyMetadata(null));
//GUI for Invent Program
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
//This button is for test purposes only
private void test_Click(object sender, RoutedEventArgs e)
{
compStatus.Icc1status = 2;
compStatus.Icc2status = 1;
}
//Routine to force color change of the system state when it changes
//I think data triggers are probably a better option
private void component_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Get the new component status
//if (compStatus.Icc1status == 0) icc1statusLabel.Background = new SolidColorBrush(Colors.Green);
//if (compStatus.Icc1status == 1) icc1statusLabel.Background = new SolidColorBrush(Colors.Yellow);
//if (compStatus.Icc1status == 2) icc1statusLabel.Background = new SolidColorBrush(Colors.Red);
}
}
Binding paths are relative to your DataContext
, so change the DataContext
from this
to compStatus
. Also, get rid of your myComponentProperty
, as it doesn't seem to serve any purpose here.