Click here to Skip to main content
15,891,951 members
Articles / Desktop Programming / WPF

Using MVC to Unit Test WPF Applications

Rate me:
Please Sign up or sign in to vote.
4.98/5 (68 votes)
27 Jan 2008CPOL23 min read 361.7K   2.6K   198  
Provides guidance for using the Model-View-Controller design pattern to create modular WPF apps that are easy to unit test
using System.Windows;
using System.Windows.Input;
using TestableXrayDemo.Controllers;
using TestableXrayDemo.Model;

namespace TestableXrayDemo
{
    /// <summary>
    /// The main Window of the demo app.
    /// </summary>
    public partial class XrayWindow : Window
    {
        #region Data

        readonly XrayWindowController _controller;

        #endregion // Data

        #region Constructor

        public XrayWindow()
        { 
            InitializeComponent();

            XrayCollection xrays = XrayCollection.Load();

            // Create the controller that  
            // handles user interaction.
            _controller = new XrayWindowController(this, xrays);

            // Use the list of Xray objects as 
            // this Window's data source.
            base.DataContext = xrays;
        }

        #endregion // Constructor

        #region Command Sinks

        // These methods handle events of  
        // the ShowSelectedXray command.

        void ShowSelectedXray_CanExecute(
            object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _controller.CanShowSelectedXray;
        }

        void ShowSelectedXray_Executed(
            object sender, ExecutedRoutedEventArgs e)
        {
            _controller.ShowSelectedXray();
        }

        #endregion // Command Sinks
    }
}

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 (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions