Code Behind or Behavior, which to use? RRS feed

  • Question

  • My app has a a layout manager with docking window panes.

    On one pane is a TreeView, and when a TreeView Item is clicked, a new docking window should dynamically be created.

    Currently, I am using a behavior to wire the TreeViewItem clicked event to a viewmodel which loads data depending on what was clicked.

    After the data is loaded by the ViewModel, I want to dynamically create a new docking window, and assign a data context to it.

    I am thinking about creating an event in the ViewModel, like CreateNewWindowEvent, which the view's code behind can subscribe to, fire this event from the ViewModel after the data is loaded, and then create the new docking window in the code behind when it recieves this event from the ViewModel.

    Is there a better way of doing this without having to put code in code behind, or is code behind a good place for dynamically created views?


    Sunday, April 3, 2011 12:53 PM


  • In your case you have to notify the view after loading asyncronously your data (you want to assign these data as datacontext of your new panel) so raise an event to UI can be the right solution. To mantain the view and viewmodel loosely coupled (the view has to subcribe to viewmodel event) i suggest to use an intermediate class ViewModelManager where you can host your events.

    Sunday, April 3, 2011 3:24 PM