如何将Label.Background绑定到变量

如何将Label.Background绑定到变量

问题描述:

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

这里是我的类:

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

绑定路径是相对于你的DataContext,所以更改DataContextthiscompStatus。此外,摆脱您的myComponentProperty,因为它似乎没有任何用途。

+0

完美地工作。感谢您及时的回复。我被困在这个3天。 – mkk 2014-11-03 17:58:24

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

this.DataContext = compStatus;