Although somewhat experienced with writing Winforms applications, the... \"vagueness\" of WPF still eludes me in terms of best practices and design patterns.
Despite pop
Others have already made useful suggestions (use an observable collection to get list-change notification, make the collection a property rather than a field). Here are two they haven't:
1) Whenever you're having a problem with data binding, look in the Output window to make sure that you're not getting any binding errors. You can spend a lot of time trying to fix the wrong problem if you don't do this.
2) Understand the role change notification plays in binding. Changes in your data source can't and won't get propagated to the UI unless the data source implements change notification. There are two ways to do this for normal properties: make the data source derive from DependencyObject
and make the bound property a dependency property, or make the data source implement INotifyPropertyChanged
and raise the PropertyChanged
event when the property's value changes. When binding an ItemsControl
to a collection, use a collection class that implements INotifyCollectionChanged
(like ObservableCollection
), so that changes to the contents and order of the collection will get propagated to the bound control. (Note that if you want changes to the items in the collection to get propagated to the bound controls, those items need to implement change notification too.)