You do not need an ObservableCollection<T>
that is only useful when you add or remove items from the collection after assigning it - which you say you do not. A List<T>
would be fine and well within MVVM. However you still have to remember to implement INotifyPropertyChanged
and raise the PropertyChanged
when the List<T>
is assigned - or anything you assign you want the binding to re-read the source.
e.g. you should have:
private List<Code> codes;
public List<Codes> Codes
{
get {return codes;}
set
{
codes = value;
NotifyPropertyChanged("Codes");
}
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
The same applies to your nested collection SubCodes, i.e. Codes
should also implement the interface and the setter for SubCodes should raise the event. Furthermore if you were to modify a Code
's property at runtime and you want that reflected in the UI the properties on Code
should raise the event too. Also any class you bind to should always implement the interface even if the individual properties do not raise the event, because binding to a class that does not implement the interface creates memory leaks. (unless the Property is a DependencyProperty, which it shouldn't be unless you are authoring you own control, or the Binding Mode is OneTime).