English 中文(简体)
Mvvm 采用模型修缮的财产
原标题:Mvvm nested propertychanged in model

我使用A类模型,其中列有B类模型清单。 每个模型(A和B)都采用该模型,并在适当变化时正确发射这些模型。 示范法 一家公司从B模型中同意了这一活动,如果发生这种事件,A型火力就会改变。

我的问题是,我想听一下A模式(包括坚固的模型特性)在服务类别中的变化,这一变化应当写上对数据库的改动。 但是,当模型A大火改变财产时,我无法知道改变的财产是否属于模型A或模型B之一。

public class Model B : ObservableObject
{
...
  public int Property1
  {
    get{return property1;}
    set{ this.property1 = value;
         OnPropertyChanged("Property1");  
       }
  }
} 


public class ModelA : ObservableObject
{
 ...
 ObservableCollection<ModelB> modelsB;
 ...

 public ObservableCollection<ModelB> ModelsB
 {
   get
   {
     if(modelsB == null)
     {
       this.modelsB = new ObservableCollection<ModelB>();
       this.modelsB.CollectionChanged+= ModelBListChanged;
     }
     return modelsB;
   }
 }


  private int property2;
  public int Property2
  {
    get{return property2;}
    set{ this.property2 = value;
         OnPropertyChanged("Property2");  
       }
  }

 private void ModelBListChanged(object sender, NotifyCollectionChangedEventArgs args)
 {
    if(e.NewItems != null)
      {
        foreach(ObservableObject item in e.NewItems)
        {
          item.PropertyChanged += NotifyPropertyChanged;
        }
      }
      if (e.OldItems != null)
      {
        foreach (ObservableObject item in e.OldItems)
        {
          item.PropertyChanged -= NotifyPropertyChanged;
        }
      }
 }

 ...

}


public class SomeService
{
   ...

  ObservableCollection<ModelA> modelsA;

  public ObservableCollection<ModelA> ModelsA
  {
     get
     {
       if(modelsA == null)
       {
         modelsA = new ObservableCollection<ModelA>();

             //fill with data...
         ....
         foreach(...)
         {
        ModelB mb = new ModelB();
            //fill mb ...
        ...

        mb.PropertyChanged += ModelBChanged;
        modelsA.ModelsB.Add(mb);
         }
         ....
         modelsA.PropertyChanged += ModelsAChanged;
       }
       return modelsA;
     }
  }

  ...

  private void ModelsAChanged(object sender, PropertyChangedEventArgs args)
  {
    // determine the property that has changed and call the correct
    // update function for this property to write the data to database.

var ma = (ModelA) sender;

      switch(args.PropertyName)
      {
     ...
        case ModelA.Property1:
          //update database with function for property 1
          break;
        case ModelA.Property2:
          //update database with function for property 2
          break;
     ...
      }
  }

  private void ModelBChanged(object sender, PropertyChangedEventArgs args)
  {
    // How do I know to which ModelA this ModelB sender belongs?

var mb = (ModelB) sender;
switch(args.PropertyName)
      {
     ...
        case ModelB.Property1:
          //update database with function for property 1 of model B
          break;
       ...
      }
  }
}

如何解决这一问题?

关于

tabina

问题回答

我不敢确保你目前的法典甚至汇编成册? 您正在将<代码>CollectionChanged从您的B Collection转成PropertyChanged关于您的A类活动,但请参阅PropertyName,该编码存在于

即使你可以找到工作,但自<条码> 编码> 栏目以来,它几乎没有意义,可参考多个项目,而<条码>上载的<<>propertyChanged仅供参考。

你的B项收藏已经是A的公共财产。 为什么你能直接赞同你服务的变化? 例如。

public class SomeService
{
    ObservableCollection<ModelA> modelsA;

    public ObservableCollection<ModelA> ModelsA
    {
        get
        {
            if(modelsA == null)
            {
               modelsA = new ObservableCollection<ModelA>();
               //fill with data...

               modelsA.CollectionChanged += ModelsAChanged;

               foreach (ModelA A in modelsA)
                   A.ModelsB.CollectionChanged += ModelsBChanged;
            }

            return modelsA;
        }
    }

    private void ModelsAChanged(object sender, NotifyCollectionChangedEventArgs args)
    {
        //remember to subscribe to ModelsB in any new ModelA instance that get added 
        //to the modelsA collection too, and unsubscribe when they get removed.
    }

    private void ModelsBChanged(object sender, NotifyCollectionChangedEventArgs args)
    {

    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签