Solved

Help with MVVM in WPF C#

Posted on 2014-12-06
9
180 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:slightlyoff
Comment Utility
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
Comment Utility
Theres a typo in XAML "UpdateCommad" should be "UpdateCommand"
0
 
LVL 1

Author Comment

by:slightlyoff
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

11 Experts available now in Live!

Get 1:1 Help Now