none
Drop a ViewModel

    Question

  • I'm working on my first MVVM application and had a question. I have a section of the window that has it's own view and subsequent ViewModel.  This view has a cancel button that would simply clear all the input fields on the view.  And I thought, instead of clearing each and every ViewModel property (there are 42), why not use a click event to set the view's DataContext to a new ViewModel.

    Besides the oft-stated mantra of "NO CODE IN THE CODE-BEHIND!" is there any reason why this would be a bad idea?  Does the GC eventually clean up the old ViewModel?

    Thanks for any input!

    Wednesday, February 16, 2011 10:16 PM

Answers

All replies

  • Hi Nevas92,

    The only problem would be that you wouldn't be able to test this logic. I would rather create a command in view model. In the execute method of this, I would be clearling the view model's properties which should reflect in the view.

    I think by "section of a window" you mean a user control.

    The other issue is the construction of view. You might be constructing the view model first and then passing it in the constructor of the view. You might lose the visibility in other part of the project once you re-instantiate it.

    Thanks,
    Muhammad
    http://shujaatsiddiqi.blogspot.com


    Muhammad Siddiqi
    Wednesday, February 16, 2011 10:49 PM
  •  

    Besides the oft-stated mantra of "NO CODE IN THE CODE-BEHIND!" is there any reason why this would be a bad idea?  Does the GC eventually clean up the old ViewModel?

     

    As long as the ViewModel doesn't rely on other objects, this would be fine.  The garbage collector will clean it up (at some point), so you don't have to worry about memory issues.

     

    If the VM is internal, and only used for that portion of the model, this may be a reasonable approach.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, February 16, 2011 10:50 PM
    Moderator
  • Hey Nevas,

    I was intrigued by your problem and ended up writing an article that explains how you can do this by using a kind of proxy view model class that wraps the underlying view model object. The class has been written in a way that lets you use your existing view / view model without any changes to them.


    http://blog.voidnish.com
    • Marked as answer by Nevas92 Thursday, February 17, 2011 12:32 PM
    Thursday, February 17, 2011 4:46 AM
    Moderator
  • Love the idea of a resettable ViewModel, wish I had thought of that.  This first application that I did was really a test run into the MVVM architecture for me, and the app is small enough that using the code behind approach works just fine for now.  However, my next project is going to be substantially bigger so I will definitely try out the resettable ViewModel idea.

    Thanks for the help fellas!

    Thursday, February 17, 2011 12:36 PM
  • You are welcome.
    http://blog.voidnish.com
    Thursday, February 17, 2011 12:48 PM
    Moderator