Click here to Skip to main content
11,501,513 members (76,463 online)
Click here to Skip to main content

MVVM Delegate Command with an ObservableCollection

, 18 Sep 2010 CPOL 9K 3
Rate this:
Please Sign up or sign in to vote.
Loading from webservice and after a processing update the UI
First of all, I get the DelegateCommand from the .toolbox courses (I encourage you to download them and find it).

Using this DelegateCommand which we bind to a button Command for instance (I always add CommandParameter in XAML too (in order to work fine).

Now my layoutroot datacontext viewmodel has the following property:

private ObservableCollection _sales;
public ObservableCollection Sales
{
  get { return _sales; }
  set
      {
         _sales= value;
         NotifyPropertyChanged(() => Sales);
         }
        }

And now (in this case, it is a WPF application, in case you use SL I recommend you to use PagedViewCollection), I instance the command in the constructor:

PaidCommand = new DelegateCommand()
{
 ExecuteCommand = () =>
   {
      bw = new BackgroundWorker();
      bw.RunWorkerCompleted += (s, e) =>
        {
          NotifyPropertyChanged(() => Sales); //#1
        };
      bw.DoWork += (s1, e1) =>
        {
           _sales= DM.GetSales(_date1, _date2); //#2
        };
      bw.RunWorkerAsync();
    }
};

In #2, we process every line of code we want filling our private fields and when it's finished, we publish
simply Notifying them #1 (In my case, I have a datagrid with ItemsSource = {Binding Sales}).

I hope it helps you (I like this way of doing it).

A new overloaded method for the NotifyPropertyChanged method to leverage the use of expressions instead of strings:
 
        protected void NotifyPropertyChanged<T&gt(Expression<Func<T>>expression)
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null)
                throw new ArgumentException("expression must be a property expression");
            RaisePropertyChanged(memberExpression.Member.Name);
        }


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Juan Pablo G.C.
Software Developer Expediteapps
Spain Spain
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions

Deeply involved in Xamarin Forms LOB (including Azure Cloud with offline support, custom controls, dependencies) projects, WP8.1 & W10 projects, WPF modern styled projects. Portable libraries like portablePDF, portableOneDrive, portableReports and portablePrinting (using Google Printing API).


Web and apps showcase at:
Expediteapps


Take a look to my blog
Blog
Follow on   Twitter

Comments and Discussions

 
GeneralNot 100% about the method with MemberExpression Pin
Juan Pablo G.C.19-Sep-10 22:41
memberJuan Pablo G.C.19-Sep-10 22:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 19 Sep 2010
Article Copyright 2010 by Juan Pablo G.C.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid