如何将Label.Background绑定到变量

时间:2014-11-03 16:50:16

标签: c# wpf variables data-binding datatrigger

我在代码隐藏中从变量中获取多个项目的状态,并且我想在GUI中的每个选项卡上以图形方式显示这些项目的状态。我将状态显示为颜色编码标签(3种不同的颜色代表3种不同的状态)。我已经想出如何用一堆代码强制背景改变颜色,但我认为使用数据触发器会更好但是我无法让它工作。使用调试器,它会逐步执行例程并正确更改值,但屏幕上的颜色永远不会改变。顺便说一下,我只使用wpf和C#大约6周。提前谢谢你指出我的错误。

这是我的班级:

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

这是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"/>

我的代码背后:

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

2 个答案:

答案 0 :(得分:0)

绑定路径与您的DataContext相关,因此将DataContextthis更改为compStatus。另外,请删除myComponentProperty,因为它似乎没有任何用途。

答案 1 :(得分:0)

DataContext不正确。它应该设置为compStatus。

this.DataContext = compStatus;