Click here to Skip to main content
12,827,276 members (46,504 online)
Click here to Skip to main content


251 bookmarked
Posted 15 May 2010

A Practical Quick-start Tutorial on MVVM in WPF

, 21 May 2010 CPOL
This article gives a practical quick-start tutorial on MVVM in WPF for application developers.
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfModelViewDemoApplication.Commands
    /// <summary>
    /// This class facilitates associating a key binding in XAML markup to a command
    /// defined in a View Model by exposing a Command dependency property.
    /// The class derives from Freezable to work around a limitation in WPF when data-binding from XAML.
    /// </summary>
    public class CommandReference : Freezable, ICommand
        public CommandReference()
            // Blank

        public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference), new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));

        public ICommand Command
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }

        #region ICommand Members

        public bool CanExecute(object parameter)
            if (Command != null)
                return Command.CanExecute(parameter);
            return false;

        public void Execute(object parameter)

        public event EventHandler CanExecuteChanged;

        private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            CommandReference commandReference = d as CommandReference;
            ICommand oldCommand = e.OldValue as ICommand;
            ICommand newCommand = e.NewValue as ICommand;

            if (oldCommand != null)
                oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
            if (newCommand != null)
                newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;


        #region Freezable

        protected override Freezable CreateInstanceCore()
            throw new NotImplementedException();


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.


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


About the Author

Dr. Song Li
United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170326.1 | Last Updated 21 May 2010
Article Copyright 2010 by Dr. Song Li
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid