Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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# 如何将Label.Background绑定到变量_C#_Wpf_Variables_Data Binding_Datatrigger - Fatal编程技术网

C# 如何将Label.Background绑定到变量

C# 如何将Label.Background绑定到变量,c#,wpf,variables,data-binding,datatrigger,C#,Wpf,Variables,Data Binding,Datatrigger,我从代码隐藏中的变量获取多个项目的状态,我希望在GUI的每个选项卡上以图形方式显示这些项目的状态。我将状态显示为颜色编码标签3种不同的颜色代表3种不同的状态。我已经想出了如何用一堆代码强制背景改变颜色,但我认为使用数据触发器会更好,但我一直无法让它工作。使用调试器,它会逐步执行例程并正确更改值,但屏幕上的颜色永远不会更改。顺便说一句,我只和wpf和C一起工作了大约6周。提前谢谢你指出我的错误 这是我的班级: class componentStatus : INotifyPropertyChang

我从代码隐藏中的变量获取多个项目的状态,我希望在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"/>

绑定路径与DataContext相关,因此将DataContext从此状态更改为compStatus。另外,去掉myComponentProperty,因为它在这里似乎没有任何用途。

DataContext不正确。应将其设置为compStatus

this.DataContext = compStatus;

工作完美。谢谢你的快速回复。我已经被困在这三天了。
this.DataContext = compStatus;