Manipulating collections & the ViewModel pattern
- by Kragen
I'm relatively new to WPF, and I'm having trouble with what I'm fairly certain is a relatively simple problem.
I have my underlying data object, a Person:
class Person
{
public string Surname {get; set; }
public string Firstname {get; set; }
public List<Address> Addresses {get; }
}
And I wish to display and edit this object in my WPF app. To this end I've created a ViewModel that I bind to in my xaml:
class PersonViewModel
{
public string Fullname {get; }
public ObservableCollection<AddressViewModel> Addresses {get; }
}
This is fine, except when it comes to manipulating my Address collection, where I can't work out what I should be doing:
Should I add methods AddAddress, RemoveAddress etc... to my PersonViewModel class for manipulating my collection with instances of AddressViewModel
Should I just add instances of AddressViewModel to my Addresses observable collection
Both of the above seem a bit messy - is there a better way of dealing with collections?