PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/StockTrader RI/Desktop/StockTraderRI.Infrastructure/ReturnKey.cs

#
C# | 143 lines | 86 code | 15 blank | 42 comment | 14 complexity | 35d2436b66c117b9665b184a449cb1b5 MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System.Windows;
 18using System.Windows.Controls;
 19using System.Windows.Input;
 20using StockTraderRI.Infrastructure.Behaviors;
 21using System;
 22
 23namespace StockTraderRI.Infrastructure
 24{
 25    public static class ReturnKey
 26    {
 27        /// <summary>
 28        /// Command to execute on return key event.
 29        /// </summary>
 30        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
 31            "Command",
 32            typeof(ICommand),
 33            typeof(ReturnKey),
 34            new PropertyMetadata(OnSetCommandCallback));
 35
 36        /// <summary>
 37        /// Default text to set to the TextBox once the Command has been executed
 38        /// </summary>
 39        public static readonly DependencyProperty DefaultTextAfterCommandExecutionProperty = DependencyProperty.RegisterAttached(
 40            "DefaultTextAfterCommandExecution",
 41            typeof(string),
 42            typeof(ReturnKey),
 43            new PropertyMetadata(OnSetDefaultTextAfterCommandExecutionCallback));
 44
 45        private static readonly DependencyProperty ReturnCommandBehaviorProperty = DependencyProperty.RegisterAttached(
 46            "ReturnCommandBehavior",
 47            typeof(ReturnCommandBehavior),
 48            typeof(ReturnKey),
 49            null);
 50
 51        /// <summary>
 52        /// Sets the <see cref="string"/> to set to the TextBox once the Command has been executed.
 53        /// </summary>
 54        /// <param name="textBox">TextBox dependency object on which the default text will be set after the Command has been executed.</param>
 55        /// <param name="defaultText">Default text to set.</param>
 56        public static void SetDefaultTextAfterCommandExecution(TextBox textBox, string defaultText)
 57        {
 58            if (textBox == null)
 59            {
 60                throw new ArgumentNullException("textBox");
 61            }
 62
 63            textBox.SetValue(DefaultTextAfterCommandExecutionProperty, defaultText);
 64        }
 65
 66        /// <summary>
 67        /// Retrieves the default text set to the <see cref="TextBox"/> after the Command has been executed.
 68        /// </summary>
 69        /// <param name="textBox">TextBox dependency object on which the default text will be set after the Command has been executed.</param>
 70        /// <returns>Default text to set.</returns>
 71        public static string GetDefaultTextAfterCommandExecution(TextBox textBox)
 72        {
 73            if (textBox == null)
 74            {
 75                throw new ArgumentNullException("textBox");
 76            }
 77
 78            return textBox.GetValue(DefaultTextAfterCommandExecutionProperty) as string;
 79        }
 80
 81        /// <summary>
 82        /// Sets the <see cref="ICommand"/> to execute on the return key event.
 83        /// </summary>
 84        /// <param name="textBox">TextBox dependency object to attach command</param>
 85        /// <param name="command">Command to attach</param>
 86        public static void SetCommand(TextBox textBox, ICommand command)
 87        {
 88            if (textBox == null)
 89            {
 90                throw new ArgumentNullException("textBox");
 91            }
 92
 93            textBox.SetValue(CommandProperty, command);
 94        }
 95
 96        /// <summary>
 97        /// Retrieves the <see cref="ICommand"/> attached to the <see cref="TextBox"/>.
 98        /// </summary>
 99        /// <param name="textBox">TextBox containing the Command dependency property</param>
100        /// <returns>The value of the command attached</returns>
101        public static ICommand GetCommand(TextBox textBox)
102        {
103            if (textBox == null)
104            {
105                throw new ArgumentNullException("textBox");
106            }
107
108            return textBox.GetValue(CommandProperty) as ICommand;
109        }
110
111        private static void OnSetDefaultTextAfterCommandExecutionCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
112        {
113            TextBox textBox = dependencyObject as TextBox;
114            if (textBox != null)
115            {
116                ReturnCommandBehavior behavior = GetOrCreateBehavior(textBox);
117                behavior.DefaultTextAfterCommandExecution = e.NewValue as string;
118            }
119        }
120
121        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
122        {
123            TextBox textBox = dependencyObject as TextBox;
124            if (textBox != null)
125            {
126                ReturnCommandBehavior behavior = GetOrCreateBehavior(textBox);
127                behavior.Command = e.NewValue as ICommand;
128            }
129        }
130
131        private static ReturnCommandBehavior GetOrCreateBehavior(TextBox textBox)
132        {
133            ReturnCommandBehavior behavior = textBox.GetValue(ReturnCommandBehaviorProperty) as ReturnCommandBehavior;
134            if (behavior == null)
135            {
136                behavior = new ReturnCommandBehavior(textBox);
137                textBox.SetValue(ReturnCommandBehaviorProperty, behavior);
138            }
139
140            return behavior;
141        }
142    }
143}