Click here to Skip to main content
15,892,839 members
Articles / Desktop Programming / WPF

Commands in MVVM

Rate me:
Please Sign up or sign in to vote.
4.97/5 (107 votes)
3 Dec 2012CPOL15 min read 513.5K   16.9K   279  
A consistent approach to Commands, Asynchronous Commands, and Events-to-Commands for WPF, Silverlight, and WP7.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apex.MVVM;
using System.Collections.ObjectModel;

namespace ControlsSample
{
    /// <summary>
    /// The main view model.
    /// </summary>
    public class MainViewModel : ViewModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainViewModel"/> class.
        /// </summary>
        public MainViewModel()
        {
            Samples.Add(new Sample() { Name = "Apex Grid", View = new Samples.ApexGrid() });
            Samples.Add(new Sample() { Name = "Cross Button", View = new Samples.CrossButton() });
            Samples.Add(new Sample() { Name = "Padded Grid", View = new Samples.PaddedGrid() });
        }

        /// <summary>
        /// The set of samples.
        /// </summary>
        private ObservableCollection<Sample> samples = new ObservableCollection<Sample>();

        /// <summary>
        /// Gets the samples.
        /// </summary>
        public ObservableCollection<Sample> Samples
        {
            get { return samples; }
        }


        /// <summary>
        /// The selected sample property.
        /// </summary>
        private NotifyingProperty SelectedSampleProperty =
          new NotifyingProperty("SelectedSample", typeof(Sample), default(Sample));

        /// <summary>
        /// Gets or sets the selected sample.
        /// </summary>
        /// <value>
        /// The selected sample.
        /// </value>
        public Sample SelectedSample
        {
            get { return (Sample)GetValue(SelectedSampleProperty); }
            set { SetValue(SelectedSampleProperty, value); }
        }
                
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions