在C#中绑定Border.BackgroundProperty,更新问题

时间:2013-05-23 10:04:14

标签: c# wpf binding

我想在C#中绑定Border.BackgroundProperty。绑定有效但Border-Element在更改时没有更新。

UIElement(fb_CoonetionIndicator = Border):

            var indicatorElement = new InputElement("");
        indicatorElement.InputElementType = InputElementTypeEnum.Indicator;
        // verified label binding
        Binding indicatorBinding = new Binding("VerificationText");
        indicatorBinding.Source = ApplicationPersistantDataModel.Instance;
        indicatorElement.local_indicatorlabel.SetBinding(Label.ContentProperty, indicatorBinding);
        // backgroundcolor binding
        Binding colorBinding = new Binding("VerificationColor");
        colorBinding.Source = ApplicationDynamicDataModel.Instance;
        colorBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        fb_ConnectionIndicator.fb_backGroundBorder.SetBinding(Border.BackgroundProperty, colorBinding);
        fb_ConnectionIndicator.AddElementToStackPanel(indicatorElement);

的DataSource:

 public sealed class ApplicationDynamicDataModel
{
    private static ApplicationDynamicDataModel instance = new ApplicationDynamicDataModel();
    public static ApplicationDynamicDataModel Instance
    {
        get { return instance; }
        set { instance = value; }
    }

    // verification indicator color
    private SolidColorBrush _verificationColor = new SolidColorBrush(Colors.GreenYellow);
    public SolidColorBrush VerificationColor
    {
        get { return _verificationColor; }
        set
        {
            _verificationColor = value;
            OnPropertyChanged("VerificationColor");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

1 个答案:

答案 0 :(得分:0)

你的类声明应该有:INotifyPropertyChanged 来实现INotifyPropertyChanged

public sealed class ApplicationDynamicDataModel : INotifyPropertyChanged  

如果您在声明中没有它,则绑定无法订阅OnPropertyChanged事件。