Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with MVVM in WPF C#

Posted on 2014-12-06
9
Medium Priority
?
210 Views
Last Modified: 2014-12-11
I've been working on learning MVVM.  I know VB.NET a lot better than C#, but most of the resources out there are C#, so I'm trying to polish up my C# while learning MVVM... fun stuff.

I've found a bunch of tutorials out there, including this one: http://www.c-sharpcorner.com/UploadFile/raj1979/simple-mvvm-pattern-in-wpf/

I follow most of it - and I have it working.  However, I kind of want to understand the code a little bit better.  The view model: UserViewModel.cs has the following code (excerpt):

private ICommand mUpdater;
        private ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }

        private class Updater : ICommand
        {
            #region ICommand Members

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object paramater)
            {
               
            }

            #endregion
        }

Open in new window


So I understand that first we create a property of type iCommand called mUpdater.
Then we create another property called UpdateCommand. (this is bound in the view)

the get and set properties of UpdateCommand is what is really throwing me:
if mUpdater == null (I'm assuming it's null initially), it creates a new instance of Updater.

What exactly is mUpdater returning?  It looks like basically that get and set for UpdateCommand makes it into an instance of Updater with a bool property, an event and a method Execute that is empty.

But what is causing the user information to change when the button linked to UpdateCommand is clicked?
When I look at the UpdateCommand here, it doesn't look like it really should be doing anything.

Sorry if the question seems vague, I just feel like I almost have this, if I can just get my head around this last part.
My entire code is the tutorial in the link above, I didn't re-post it here because it's clearer in the tutorial.

Thanks for your help!!!
0
Comment
Question by:slightlyoff
  • 4
  • 3
  • 2
9 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40485460
>>But what is causing the user information to change when the button linked to UpdateCommand is clicked?

When you look at the full sample, you can find that the Command property is bound to the UpdateCommand. This is how the button is linked.

>>What exactly is mUpdater returning?  

mUpdater is the pointer to a Command (action and properties on this action)
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 40485467
Thanks for the reply.  I see how its linked, what I dont see is where its being told what action to perform "on-click".

Its probably obvious,  but I'm missing it.

Thanks again for your help!
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40485508
mUpdater is assigned to the Updater class:
 if (mUpdater == null)
                    mUpdater = new Updater();

Open in new window


And the Updater class has an Excute Method.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:ambience
ID: 40487125
This tutorial is actually not a good example of, at least, how to use ICommand. The whole magic is really the two way data-binding in action - because the SelectedItem of the List is bound to the Form on top. You should see that the functionality doesn't change even if you don't click the Update button or just remove it altogether. Due to two-way binding (the default) WPF will automatically reflect changes to the SelectedItem of the List as soon as you change focus across text fields.

Why don't you try to add some of your own code to the Execute method of the Updater class, a simple MessageBox would do just to see how Command routing works?

Another useful exercise that would definitely help understand the stuff better would be to try to add "Add new User" button that should allow you to Add a new item to list. A "Remove" button for deletion.
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 40487164
I see what you mean.  In effect, the Update button isn't really doing anything.  When you click it, it takes the focus, and when the focus is changed, the ListView is updated regardless of what's going on in the ViewModel.

I took your suggestion to add a messagebox to the execute method.  My code looks like this:

        private class Updater : ICommand
        {
            #region ICommand Members

            public bool CanExecute(object parameter)
            {
                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object paramater)
            {
                MessageBox.Show("Hey!");
            }

            #endregion
        }

Open in new window


When I run the application and click the button, nothing happens.
Am I missing something?

Thanks for your response!
0
 
LVL 22

Expert Comment

by:ambience
ID: 40487336
Theres a typo in XAML "UpdateCommad" should be "UpdateCommand"
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 40487360
Thanks again for the response, and I appreciate your time.  I fixed the typo, but still nothing happens on click.
0
 
LVL 22

Accepted Solution

by:
ambience earned 2000 total points
ID: 40488490
Thats odd, just attached a working version of the code from the same tutorial that works.
WpfApplication2.zip
0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 40495387
Thank you for your help.
A couple of things...

1. I misspelled "parameter"
2. I had the methods private, where you had them public.  Once I changed that it worked.

Thanks for your help!!!
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question