This is a migrated thread and some comments may be shown as answers.

MVVM vs Events

5 Answers 101 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 27 Jul 2011, 08:44 PM

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.

5 Answers, 1 is accepted

Sort by
0
hwsoderlund
Top achievements
Rank 1
answered on 28 Jul 2011, 09:09 AM
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.
0
Andrew
Top achievements
Rank 1
answered on 28 Jul 2011, 10:29 PM
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;
                }
            }
        }
0
Accepted
hwsoderlund
Top achievements
Rank 1
answered on 28 Jul 2011, 10:59 PM
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.
0
Andrew
Top achievements
Rank 1
answered on 09 Aug 2011, 01:07 PM
Thanks. I chose to move the properties to my viewmodel.

And i pass the changes back to the entity when required.
0
Dan Andrews
Top achievements
Rank 2
answered on 10 Aug 2011, 08:41 PM
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).
Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 1
Answers by
hwsoderlund
Top achievements
Rank 1
Andrew
Top achievements
Rank 1
Dan Andrews
Top achievements
Rank 2
Share this question
or