Manipulating collections & the ViewModel pattern
Posted
by Kragen
on Stack Overflow
See other posts from Stack Overflow
or by Kragen
Published on 2010-06-02T09:58:17Z
Indexed on
2010/06/02
10:23 UTC
Read the original article
Hit count: 348
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 myPersonViewModel
class for manipulating my collection with instances ofAddressViewModel
- Should I just add instances of
AddressViewModel
to myAddresses
observable collection
Both of the above seem a bit messy - is there a better way of dealing with collections?
© Stack Overflow or respective owner