Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WPF/MVVM Quick Start Tutorial

, 10 Oct 2012
A quick example of MVVM in WPF
MvvmExample.zip
MvvmExample
Example1
Properties
Settings.settings
Example2
Properties
Settings.settings
Example3
Properties
Settings.settings
Example4
Properties
Settings.settings
Example5
Properties
Settings.settings
Example6
Properties
Settings.settings
MicroMvvm
Properties
MvvmExample.suo
using System;
using System.Diagnostics;
using System.Windows.Input;

namespace Example3
{
    public class RelayCommand : ICommand
    {
        #region Members
        readonly Func<Boolean> _canExecute;
        readonly Action _execute;
        #endregion

        #region Constructors
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action execute, Func<Boolean> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion

        #region ICommand Members
        public event EventHandler CanExecuteChanged
        {
            add
            {

                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {

                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        [DebuggerStepThrough]
        public Boolean CanExecute(Object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(Object parameter)
        {
            _execute();
        }
        #endregion
    }

}

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)

Share

About the Author

Barry Lapthorn

United Kingdom United Kingdom
Jack of all trades.

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 10 Oct 2012
Article Copyright 2011 by Barry Lapthorn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid