Solved

Help with MVVM in WPF C#

Posted on 2014-12-06
9
182 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 69

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 69

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Search by two fields at the same time using c# radio button 5 39
COnsume rest client 6 36
Angular JS Route 3 54
C# parent child form 5 19
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now