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