A part of the editor i’m writing uses a Wpf-TreeView. I’m using DataBinding and an ItemTemplate to populate the TreeView. So far i’m manipulating the ItemsSource(mostly ObeservableCollection’s) dircetly(using for example Drag&Drop). But now i read this and i’m not sure if it would realy simplify thinks for me. And before i go on with the project i whould like to know all Pros and Cons.
If Data(ItemsSource) is added, edited or delete, how to keep the Data and the ViewModel consistent? Is that something the ViewModel has to take care of? If i have to take care of the consistens how does this simplifies thinks?
MVVM is a great fit for WPF development in general, not just in
TreeViews.Not sure exactly what you’re asking here, but WPF binding handles collection changes, as long as those collection implement
INotifyCollectionChanged.ObservableCollection<T>gives you a nice, useful implementation of this interface that you can use within your view models.Bindings keep the view consistent with your view model. Generally what you’re aiming for is zero code-behind in your view. Your view just binds to properties on the view model and it is the responsibility of the view model to keep related properties in sync. Here’s a really simple example:
Here the
FullNameproperty is affected by changes toFirstNameandLastName. The view can just bind toFullNameand any changes to the other two properties will be visible in the UI.I’d advise you to read my blog post on POCOs versus
DependencyObjects as view models before you start out.