Solved

Help with MVVM in WPF C#

Posted on 2014-12-06
9
185 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-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 hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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