MVVM vs Events

6 posts, 1 answers
  1. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 27 Jul 2011 Link to this post

    Fellow forum dwellers, my rusty programing skills stem from the old day of using events and are unused within the MVVM environment. I have a very simple problem to solve.

    I have a DataForm all bound to a database via a view and RIA services.

    The form is very simple, it has a checkbox, and 3 text boxes. It was fairly easy to wire all up even with my non-existent MVVM skills.

    However I encountered a problem, I would like to have the 3rd box generate the text automatically. More precisely, if the checkbox is checked then I want the 3rd box to be populated with the values in the other two boxes multiplied by each other.

    Naturally in the old event way this would be quite easy, but I am completely stumped on how to do it within my view.

    Any help would be appreciated.

  2. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 28 Jul 2011 Link to this post

    You cannot do that within your view alone. The calculation must happen in the viewmodel. One approach would be the following:
    1. Bind your checkbox to a bool property in your viewmodel. Whenever that value changes to true, perform the calculation and update a property in the vm that holds the result.
    2. The textbox that displays the result must bind to the result propety in the vm.
    3. You must also bind the other two textboxes to their own int properties in the viewmodel. These must use twoway bindings.
    4. When any of the two "multiplier" properties change, you must check if the bool property is true. If it is, perform the calculation and update the result property.
  3. DevCraft banner
  4. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 28 Jul 2011 Link to this post

    I am sorry, that was a typo. the second last sentance should have ended with ViewModel not View.

    All 4 elements are already bound to an entity object within my ViewModel.

    The problem i am having is with step 4.

    "When any of the two properties change ... " how do i know they change?

    There is no event in my ViewModel.

    The Entity Obejct i am binding the controls to is:

     

    public OwlRecord NewOwlRecord
            {
                get { return _newOwlRecord; }
                set
                {
                    if (value != _newOwlRecord)
                    {
                        _newOwlRecord = value;
                    }
                }
            }
  5. Answer
    hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 28 Jul 2011 Link to this post

    Ok, if there is an entity involved that makes it a little more complicated. If all these 4 properties are a part of the entity you can just build the logic into the entity itself. If you take that approach you would add code into the property setters of your entity that would call methods within the entity that would update the result property. If, on the other hand, your properties reside in different places, ie. some in the entity and some in the viewmodel, my approach would be to move all the logic and all 4 properties to the viewmodel. Then at the time of saving the entity I would take the values from the viewmodel and update the state of the entity.

    So basically, instead of listening to events you listen to the changes in the property setters, either in your viewmodel or your entities.
  6. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 09 Aug 2011 Link to this post

    Thanks. I chose to move the properties to my viewmodel.

    And i pass the changes back to the entity when required.
  7. Dan Andrews
    Dan Andrews avatar
    28 posts
    Member since:
    Jul 2009

    Posted 10 Aug 2011 Link to this post

    Since this has been addressed, I will only add levity.  Once you embrace MVVM, you'll treat it as your religion.  It is very difficult at first but once it clicks, you'll wonder why you never did it that way.  Especially when you add messages to communicate through different ViewModels, then you won't need a ViewModelLocator (as much).
Back to Top
DevCraft banner