1

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

UIElement(fb_CoonetionIndicator = 边框):

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

数据源:

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

1 回答 1

0

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

public sealed class ApplicationDynamicDataModel : INotifyPropertyChanged  

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

于 2013-05-23T10:26:16.320 回答