Click here to Skip to main content
15,897,273 members
Articles / Desktop Programming / WPF

WPF.JoshSmith

Rate me:
Please Sign up or sign in to vote.
4.99/5 (51 votes)
13 Jul 2008CPOL5 min read 391.8K   4.8K   263  
A free library of controls and utility classes for use in WPF applications.
// Copyright (C) Josh Smith - July 2008
using System;
using System.Windows;
using System.Windows.Input;

namespace WPF.JoshSmith.Input
{
    /// <summary>
    /// This abstract class is a RoutedCommand which allows its
    /// subclasses to provide default logic for determining if 
    /// they can execute and how to execute.  To enable the default
    /// logic to be used, set the IsCommandSink attached property
    /// to true on the root element of the element tree which uses 
    /// one or more SmartRoutedCommand subclasses.
    /// </summary>
    /// <remarks>
    /// Documentation: http://www.codeproject.com/KB/WPF/SmartRoutedCommandsInWPF.aspx
    /// </remarks>
    public abstract class SmartRoutedCommand : RoutedCommand
    {
        #region IsCommandSink

        /// <summary>
        /// Gets the value of the attached IsCommandSink property for the specified object.
        /// </summary>
        public static bool GetIsCommandSink(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsCommandSinkProperty);
        }

        /// <summary>
        /// Sets the value of the attached IsCommandSink property for the specified object.
        /// </summary>
        public static void SetIsCommandSink(DependencyObject obj, bool value)
        {
            obj.SetValue(IsCommandSinkProperty, value);
        }

        /// <summary>
        /// Represents the IsCommandSink attached property.  This field is readonly.
        /// </summary>
        public static readonly DependencyProperty IsCommandSinkProperty =
         DependencyProperty.RegisterAttached(
         "IsCommandSink",
         typeof(bool),
         typeof(SmartRoutedCommand),
         new UIPropertyMetadata(false, OnIsCommandSinkChanged));

        /// <summary>
        /// Invoked when the IsCommandSink attached property is set on an element.
        /// </summary>
        /// <param name="depObj">The element on which the property was set.</param>
        /// <param name="e">Information about the property setting.</param>
        static void OnIsCommandSinkChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            bool isCommandSink = (bool)e.NewValue;

            UIElement sinkElem = depObj as UIElement;
            if (sinkElem == null)
                throw new ArgumentException("Target object must be a UIElement.");

            if (isCommandSink)
            {
                CommandManager.AddCanExecuteHandler(sinkElem, OnCanExecute);
                CommandManager.AddExecutedHandler(sinkElem, OnExecuted);
            }
            else
            {
                CommandManager.RemoveCanExecuteHandler(sinkElem, OnCanExecute);
                CommandManager.RemoveExecutedHandler(sinkElem, OnExecuted);
            }
        }

        #endregion // IsCommandSink

        #region Static Callbacks

        static void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            SmartRoutedCommand cmd = e.Command as SmartRoutedCommand;
            if (cmd != null)
            {
                e.CanExecute = cmd.CanExecuteCore(e.Parameter);
            }
        }

        static void OnExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            SmartRoutedCommand cmd = e.Command as SmartRoutedCommand;
            if (cmd != null)
            {
                cmd.ExecuteCore(e.Parameter);
                e.Handled = true;
            }
        }

        #endregion // Static Callbacks

        #region Abstract Methods

        /// <summary>
        /// Child classes override this method to provide logic which
        /// determines if the command can execute.  This method will 
        /// only be invoked if no element in the tree indicated that
        /// it can execute the command.
        /// </summary>
        /// <param name="parameter">The command parameter (optional).</param>
        /// <returns>True if the command can be executed, else false.</returns>
        protected abstract bool CanExecuteCore(object parameter);

        /// <summary>
        /// Child classes override this method to provide default 
        /// execution logic.  This method will only be invoked if
        /// CanExecuteCore returns true.
        /// </summary>
        /// <param name="parameter">The command parameter (optional).</param>
        protected abstract void ExecuteCore(object parameter);

        #endregion // Abstract Methods
    }
}

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