- wpfjoshsmith_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
- Test.DragCanvas
- Test.ListViewDragDropManager
- Test.RegexValidator
- Test.SlidingListBox
- Test.SmartTextBox
- Test.UnloadedManager
- Test.ValueConverterGroup
- WPF.JoshSmith.sln
- WPF.JoshSmith
- WPF.JoshSmith_src.zip
- WPFJoshSmith.zip
|
// 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.