如何将Label.Background绑定到变量

本文关键字:绑定 变量 Background Label | 更新日期: 2023-09-27 18:28:52

我正在从代码后面的变量中获取多个项目的状态,我想在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);
    }
}

如何将Label.Background绑定到变量

绑定路径相对于DataContext,因此将DataContextthis更改为compStatus。另外,去掉你的myComponentProperty,因为它在这里似乎没有任何作用。

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

this.DataContext = compStatus;