简体   繁体   English

实现许多INotifyPropertyChanged

[英]Implement many INotifyPropertyChanged

please tell me best way to implement many duplicate INotifyPropertyChanged. 请告诉我实现许多重复INotifyPropertyChanged的最佳方法。 I have a MainClass that has 10 children, every child has six field and every field must fired property change when own value changed. 我有一个有10个孩子的MainClass,每个孩子有6​​个字段,并且每个字段必须在更改自身值时触发属性更改。 this my code but not work: 这是我的代码,但不起作用:

    public class BaseModel
{

    public string S1 { get; set; }
    public string S2 { get; set; }
    public string S3 { get; set; }
    public string S4 { get; set; }
    public string S5 { get; set; }
    public string S6 { get; set; }
}

and I use a class named ViewModelBase to implement INotifyPropertyChanged. 我使用一个名为ViewModelBase的类来实现INotifyPropertyChanged。 in second step use a class to implement duplicate INotifyPropertyChanged: 在第二步中,使用一个类来实现重复的INotifyPropertyChanged:

    public class ImplementBaseModel : ViewModelBase
{
    private readonly BaseModel _baseModel;
    public ImplementBaseModel()
    {
        _baseModel = new BaseModel();
    }

    public string S1
    {
        get { return _baseModel.S1; }
        set
        {
            if (_baseModel.S1 == value)
                return;

            _baseModel.S1 = value;
            base.OnPropertyChanged("S1");
        }
    }
    public string S2
    {
        get { return _baseModel.S2; }
        set
        {
            if (_baseModel.S2 == value)
                return;
            _baseModel.S1 = value;
            base.OnPropertyChanged("S2");
        }
    }
    // other code...

}

then a model has 10 of this class: 那么一个模型有10个此类:

    public class MidClass
{
    public ImplementBaseModel ImplementBaseModel1 { get; set; }
    public ImplementBaseModel ImplementBaseModel2 { get; set; }
   // other field
    public ImplementBaseModel ImplementBaseModel10 { get; set; }

    public MidClass()
    {
        ImplementBaseModel1 = new ImplementBaseModel();
        ImplementBaseModel2 = new ImplementBaseModel();
        // ....
        ImplementBaseModel10 = new ImplementBaseModel();

    }
}

OK finish code! OK完成代码! now please tell me why some property not fired when value change? 现在,请告诉我为什么价值变动时某些财产没有被解雇? is a best way to implement this code? 是实现此代码的最佳方法?

In your setters, you never actually set the value. 在设置员中,您实际上从未设置过该值。 Use: 采用:

public string S1
{
    get { return _baseModel.S1; }
    set
    {
        if (_baseModel.S1 == value)
            return;
        baseModel.S1 = value;
        OnPropertyChanged("S1");
    }
}

Note that I removed the base from OnPropertyChanged. 请注意,我从OnPropertyChanged中删除了base It isn't normal to invoke the PropertyChanged event in this way. 以这种方式调用PropertyChanged事件是不正常的。

All NotifyPropertyChanged does is cause every binding to perform a "get" on their bound property. NotifyPropertyChanged所做的所有操作是使每个绑定对其绑定属性执行“获取”。 If the backing field is never updated, they will just get the same data. 如果支持字段从未更新过,它们将仅获得相同的数据。

as a shortcut, you could also create a local method like 作为快捷方式,您还可以创建一个本地方法,例如

bool UpdateAndRaiseIfNecessary( ref string baseValue, string newValue, [CallerMemberName] string propertyName = null)
{
    if (baseValue != newValue)
    {
        baseValue = newValue;
        OnPropertyChanged( propertyName );
        return true;
    }

    return false;
}

and then all of the setters would be like this: 然后所有的二传手都会是这样的:

set
{
    this.UpdateAndRaiseIfNecessary( ref _baseModel.S1, value );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM