【问题标题】:Implement many INotifyPropertyChanged实现许多 INotifyPropertyChanged
【发布时间】:2014-06-02 08:01:04
【问题描述】:

请告诉我实现许多重复的 INotifyPropertyChanged 的​​最佳方法。 我有一个有 10 个孩子的 MainClass,每个孩子有六个字段,当自己的值发生变化时,每个字段都必须触发属性更改。 这是我的代码,但不起作用:

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

我使用一个名为 ViewModelBase 的类来实现 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...

}

那么一个模型有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 完成代码!现在请告诉我为什么某些属性在值更改时没有被触发?是实现此代码的最佳方法吗?

【问题讨论】:

  • 不要使用最好这个词。通常没有最佳解决方案。
  • 对不起 BradleyDotNET 我编辑它。但没有这不是解决方案
  • 您使用base 关键字来访问已更改的属性似乎也有点奇怪。只是为了确保,您是否真的击中了属性更改事件触发(使用正确的值)?
  • 感谢 BradleyDotNET 的每一个想法。
  • 基本关键字是问题吗?

标签: c# wpf oop


【解决方案1】:

作为一种快捷方式,您还可以创建一个本地方法,例如

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

    return false;
}

然后所有的setter都是这样的:

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

【讨论】:

    【解决方案2】:

    在您的设置器中,您从未真正设置过值。使用:

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

    请注意,我从 OnPropertyChanged 中删除了 base。以这种方式调用 PropertyChanged 事件是不正常的。

    所有 NotifyPropertyChanged 所做的就是使每个绑定对其绑定的属性执行“获取”。如果从不更新支持字段,他们将只获得相同的数据。

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 2014-09-30
      • 2011-04-08
      • 2021-06-29
      • 2012-04-05
      • 2014-07-30
      • 2018-03-29
      • 1970-01-01
      相关资源
      最近更新 更多