MVVM for collections

I recently started learning wpf and am trying to use mvvm.

I understand that in mvvm neither a view nor a model should know that there is another.

What I'm trying to do is display a list of clients on the screen. But if I encode viewModel as below. which is similar to many examples that I see on the net, then I get code similar to this

class Customer 
{    
    public String Name {get;set;}     
    public String Address {get;set;} }
}

class MainWindowViewModel
{
    ObservableCollection<Customer> customers = new ObservableCollection<Customer>();

    public ObservableCollection<Customer> Customer 
    {
      get {return customers;}
    } 

    public MainWindowViewModel() 
    {
     //cust1 and cust2 are Customer objets
      customers.Add(cust1);
      customers.Add(cust2);
    }
}

Now, if I create an instance of my MainWindowViewModel and set it as the datacontext of my MainWindowView (my view), and I also bind the viewmodels Clients to the listBox, then the view will need a reference to the assembly containing my models.

So, my questions.

1) , MVVM, , .

2) CustomerViewModel MainWindowViewModel ObservableCollection CustomerViewModel ObservableCollection . .

+5
5
  • , , , , ? , - XAML , .
  • , , , . , Age User, User DateOfBirth. UserViewModel Age , .
+3

:

  • , ? , . (Model → View) - .

  • CustomerViewModel, . .

BookLibrary WPF Application Framework (WAF), , .

+2

CustomerViewModel. CollectionViewModelBase. , , , , , UI, , POCOs.

0

MVVM MVx- (MVC, MVP,...) , (SoC), , , / . SoC MVVM :

  • ; , , , .
  • -; "", XAML .

, , , :

Model <= ViewModel <= View

, ViewModel , ViewModel. , ViewModel, .

ViewModel , - , (1) .

ViewModel - "" , . , :

Model <= View

(1) , (2) -.

, , ViewModel. :

http://www.scottlogic.co.uk/blog/colin/2009/08/the-mini-viewmodel-pattern/

, ViewModel , , !

0

You will definitely want to wrap your models only for viewing objects, as shown below:

/// <summary>
/// Business model object : Should be in your separate business model only library
/// </summary>
public class BusinessModelObject
{
    public string Prop1 { get; set; }
    public int Prop2 { get; set; }
}



/// <summary>
/// Base notifying object : Should be in your GUI library
/// </summary>
public abstract class NotifyingObject<T> : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }


    private static readonly PropertyChangedEventArgs ModelPropertyChanged = new PropertyChangedEventArgs("Model");
    private T _model;
    public T Model
    {
        get { return _model; }
        set
        {
            _model = value;
            NotifyPropertyChanged(ModelPropertyChanged);
        }
    }
}

/// <summary>
/// Model decorator : Should be in your GUI library
/// </summary>
public class BusinessModelObjectAdapter : NotifyingObject<BusinessModelObject>
{
    public BusinessModelObjectAdapter(BusinessModelObject model)
    {
        this.Model = Model;
    }

    private static readonly PropertyChangedEventArgs Prop1PropertyChanged = new PropertyChangedEventArgs("Prop1");
    private string _prop1;
    public string Prop1
    {
        get { return Model.Prop1; }
        set
        {
            Model.Prop1 = value;
            NotifyPropertyChanged(Prop1PropertyChanged);
        }
    }

    private static readonly PropertyChangedEventArgs Prop2PropertyChanged = new PropertyChangedEventArgs("Prop2");
    private int _prop2;
    public int Prop2
    {
        get { return Model.Prop2; }
        set
        {
            Model.Prop2 = value;
            NotifyPropertyChanged(Prop1PropertyChanged);
        }
    }

    //and here you can add whatever property aimed a presenting your model without altering it at any time
}
0
source

All Articles