PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/V2/trunk/RI/Desktop/StockTraderRI.Infrastructure/Behaviors/ReturnCommandBehavior.cs

#
C# | 83 lines | 45 code | 7 blank | 31 comment | 13 complexity | 94a923af59f9c3a5149b93f7f5f84b83 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.Controls;
18using System.Windows.Input;
19using Microsoft.Practices.Composite.Presentation.Commands;
20
21namespace StockTraderRI.Infrastructure.Behaviors
22{
23    /// <summary>
24    /// Defines a behavior that executes a <see cref="ICommand"/> when the Return key is pressed inside a <see cref="TextBox"/>.
25    /// </summary>
26    /// <remarks>This behavior also supports setting a basic watermark on the <see cref="TextBox"/>.</remarks>
27    public class ReturnCommandBehavior : CommandBehaviorBase<TextBox>
28    {
29        /// <summary>
30        /// Initializes a new instance of <see cref="ReturnCommandBehavior"/>.
31        /// </summary>
32        /// <param name="textBox">The <see cref="TextBox"/> over which the <see cref="ICommand"/> will work.</param>
33        public ReturnCommandBehavior(TextBox textBox)
34            : base(textBox)
35        {
36            textBox.AcceptsReturn = false;
37            textBox.KeyDown += (s, e) => this.KeyPressed(e.Key);
38            textBox.GotFocus += (s, e) => this.GotFocus();
39            textBox.LostFocus += (s, e) => this.LostFocus();
40        }
41
42        /// <summary>
43        /// Gets or Sets the text which is set as water mark on the <see cref="TextBox"/>.
44        /// </summary>
45        public string DefaultTextAfterCommandExecution { get; set; }
46
47        /// <summary>
48        /// Executes the <see cref="ICommand"/> when <paramref name="key"/> is <see cref="Key.Enter"/>.
49        /// </summary>
50        /// <param name="key">The key pressed on the <see cref="TextBox"/>.</param>
51        protected void KeyPressed(Key key)
52        {
53            if (key == Key.Enter && TargetObject != null)
54            {
55                this.CommandParameter = TargetObject.Text;
56                ExecuteCommand();
57
58                this.ResetText();
59            }
60        }
61
62        private void GotFocus()
63        {
64            if (TargetObject != null && TargetObject.Text == this.DefaultTextAfterCommandExecution)
65            {
66                this.ResetText();
67            }
68        }
69
70        private void ResetText()
71        {
72            TargetObject.Text = string.Empty;
73        }
74
75        private void LostFocus()
76        {
77            if (TargetObject != null && string.IsNullOrEmpty(TargetObject.Text) && this.DefaultTextAfterCommandExecution != null)
78            {
79                TargetObject.Text = this.DefaultTextAfterCommandExecution;
80            }
81        }
82    }
83}