2

I currently have three models in EF (House,Room,Item).

public class House
{
   property int ID { get; set; }
   property string Name { get; set; }
   property List<Room> Rooms { get; set; }
}

public class Room
{
   property int ID { get; set; }
   property string Name { get; set; }
   property List<Item> Items { get; set; }
}

public class Item
{
   property int ID { get; set; }
   property string Name { get; set; }
}

In my UI I have a Treeview and a display area to show the items further details for modification when I double click an item in the treeview.

My treeview to improve performance calls a webservice which returns the following DTO/s

public class LayoutItemDTO
{
   property int ID { get; set; }
   property string Name { get; set; }
   property List<LayoutItemDTO> Children { get; set; }
}

these DTO's are mapped built using a query to the database on the house, room, item models.

Now when the user double clicks a house item on the treeview it calls a webservice to return the house model with the rooms collection into a view for the user to add/remove rooms and when they close the view it prompts for a save.

The same happens when the user double clicks on a room (i.e add/remove items to a room).

This all works great apart from keeping the treeview and the opened view in sync so if they change the name of a room or add/remove an item I want it reflecting in the treeview in memory and reverting if they cancel the changes on close.

Currently I have done this using the event aggregator but it seems untidy calling events for each action, if I could just use WPF binding it would all work instantly.

The reason for not using the same models on the treeview is due to these models having a lot more information on them than is shown, when obtaining everything this causes a performance problem.

1 Answer 1

0

Databinding and INotifyPropertyChanged

If you want that changes on your objects to be reflected in the UI using data binding, you have to implement INotifyPropertyChanged interface in your model class (or in a ViewModel if you are using MVVM pattern).

By implementing the interface, an event (PropertyChanged) will be triggered each time a property value is modified, and the controls databinded to the property will refresh to show the new values.

You can find an example here: How to: Implement the INotifyPropertyChanged Interface

Collections

For the collection, WPF databinding will work if the collection implements INotifyCollectionChanged. The List<T> type does not implement this interface, so the TreeView won't reflect add/removes from the list. The type ObservableCollection<T> implements this interface, so you just have to change List<LayoutItemDTO> to ObservableCollection<LayoutItemDTO> and the changes should be reflected.

MVVM

As you mentioned using MVVM, I would add that I normally would have ObservableCollection and INotifyPropertyChanged implementations in my ViewModels. You may want to create a LayoutItemViewModel that would encapsulate a LayoutItemDTO.

I can also advise you to have a look at existing toolkits and frameworks that can help a lot for implementing "plumbing code" for MVVM (like INotifyPropertyChanged implementation). I use mainly MVVM Light, but there are a lot of other availabe depending on your needs.

Here is also a good link for implementing TreeView databinding in a MVVM manner: Simplifying the WPF TreeView by Using the ViewModel Pattern

1
  • Hi thanks for the response we do have viewmodels for these DTO's so the treeview has the layoutviewmodel and the view when this is open has the houseviewmodel with nested viewmodels for the rooms and the items these are built after the user has double clicked on the treeview and the second service call has been made. The problem comes into play when we change any of these items in the treeview or the opened view with different viewmodels behind them. We need a way to somehow have a common related viewmodel between the two
    – jclarkson
    Oct 28, 2014 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.