标签数据绑定不更新

时间:2014-12-17 07:58:21

标签: c# winforms data-binding

我是对Windows窗体中Label的Text属性的readonly属性的DataBinding。

lblSourceLoggingState.DataBindings.Add("Text", machine, "Status");

我的问题是文本只在表单启动时更新。 我正在这样的基类中实现INotifyPropertyChanged:

    protected void SetValue<T>(Expression<Func<T>> property, T value)
    {
        LambdaExpression lambdaExpression = property;

        if (lambdaExpression == null)
        {
            throw new ArgumentException("Lambda expression return value can't be null", "property");
        }

        string propertyName = PropertyName.GetMemberName(lambdaExpression);

        T storedValue = getValue<T>(propertyName);

        if (Equals(storedValue, value))
            return;

        _propertyValueStorage[propertyName] = value;
        OnPropertyChanged(propertyName);
}

public SynchronizationContext SyncContext;
protected ViewModelBase()
{
    SyncContext = SynchronizationContext.Current;
    _propertyValueStorage = new Dictionary<string, object>();
}


protected void OnPropertyChanged(string propertyName)
{
    var handler = PropertyChanged;
    if (handler != null)
    {
        if (SyncContext != null)
            SyncContext.Send(obj => handler(this, new PropertyChangedEventArgs(propertyName));
        else
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这里是状​​态属性的机器对象中的代码:

    public short Status
    {
        get { return GetValue(() => Status); }
        private set { SetValue(() => Status, value); }
    }

在调试和单步执行SetValue或OnPropertyChanged方法时,Label文本会更新,但如果我运行没有断点的应用程序,它就不会。 如果我像这样注册PropertyChangedEvent:

        machine.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
        {
            if (args.PropertyName == "Status")
                lblSourceLoggingState.Text = machine.Status.ToString();
        };

标签文本也会像它应该更新一样,那么DataBindings会出现什么问题?

0 个答案:

没有答案