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

WPF.JoshSmith

, 13 Jul 2008
A free library of controls and utility classes for use in WPF applications.
WPF.JoshSmith_src.zip
WPF.JoshSmith
Resources
Images
Composers
bach.jpg
beethoven.jpg
chopin.jpg
handel.jpg
haydn.jpg
mozart.jpg
part.jpg
reich.jpg
scarlatti.jpg
schoenberg.jpg
Robots
colorful robot.jpg
reflected robot.jpg
Robot up close.jpg
Robot.jpg
space robot.jpg
Steelbot.jpg
Weird Robot.jpg
Wooden Robot.jpg
Test.CenteredContentControl
harpsichord.jpg
Properties
Settings.settings
Test.DragCanvas
Images
girls.gif
Properties
Settings.settings
Test.ListViewDragDropManager
Properties
Settings.settings
Test.RegexValidator
Properties
Settings.settings
Test.SlidingListBox
Properties
Settings.settings
Test.SmartTextBox
Properties
Settings.settings
Test.UnloadedManager
Properties
Settings.settings
TestPages
Test.ValueConverterGroup
Properties
Settings.settings
WPF.JoshSmith
Adorners
Controls
Utilities
Validation
Data
ValueConverters
Input
Markup
Panels
Properties
Settings.settings
ServiceProviders
UI
WPFJoshSmith.zip
bach.jpg
beethoven.jpg
chopin.jpg
handel.jpg
haydn.jpg
mozart.jpg
part.jpg
reich.jpg
scarlatti.jpg
schoenberg.jpg
colorful robot.jpg
reflected robot.jpg
Robot up close.jpg
Robot.jpg
space robot.jpg
Steelbot.jpg
Weird Robot.jpg
Wooden Robot.jpg
harpsichord.jpg
Settings.settings
girls.gif
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
WPFJoshSmith_src.zip
bach.jpg
beethoven.jpg
chopin.jpg
handel.jpg
haydn.jpg
mozart.jpg
part.jpg
reich.jpg
scarlatti.jpg
schoenberg.jpg
colorful robot.jpg
reflected robot.jpg
Robot up close.jpg
Robot.jpg
space robot.jpg
Steelbot.jpg
Weird Robot.jpg
Wooden Robot.jpg
harpsichord.jpg
Settings.settings
girls.gif
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
Settings.settings
// 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)

Share

About the Author

Josh Smith
Software Developer (Senior) Cynergy Systems
United States United States
Josh creates software, for iOS and Windows.
 
He works at Cynergy Systems as a Senior Experience 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[^].
Follow on   Twitter

You may also be interested in...

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 13 Jul 2008
Article Copyright 2006 by Josh Smith
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid