Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with MVVM in WPF C#

Posted on 2014-12-06
9
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

730 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