PageRenderTime 33ms CodeModel.GetById 10ms 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. //===================================================================================
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Input;
  20. using StockTraderRI.Infrastructure.Behaviors;
  21. using System;
  22. namespace StockTraderRI.Infrastructure
  23. {
  24. public static class ReturnKey
  25. {
  26. /// <summary>
  27. /// Command to execute on return key event.
  28. /// </summary>
  29. public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
  30. "Command",
  31. typeof(ICommand),
  32. typeof(ReturnKey),
  33. new PropertyMetadata(OnSetCommandCallback));
  34. /// <summary>
  35. /// Default text to set to the TextBox once the Command has been executed
  36. /// </summary>
  37. public static readonly DependencyProperty DefaultTextAfterCommandExecutionProperty = DependencyProperty.RegisterAttached(
  38. "DefaultTextAfterCommandExecution",
  39. typeof(string),
  40. typeof(ReturnKey),
  41. new PropertyMetadata(OnSetDefaultTextAfterCommandExecutionCallback));
  42. private static readonly DependencyProperty ReturnCommandBehaviorProperty = DependencyProperty.RegisterAttached(
  43. "ReturnCommandBehavior",
  44. typeof(ReturnCommandBehavior),
  45. typeof(ReturnKey),
  46. null);
  47. /// <summary>
  48. /// Sets the <see cref="string"/> to set to the TextBox once the Command has been executed.
  49. /// </summary>
  50. /// <param name="textBox">TextBox dependency object on which the default text will be set after the Command has been executed.</param>
  51. /// <param name="defaultText">Default text to set.</param>
  52. public static void SetDefaultTextAfterCommandExecution(TextBox textBox, string defaultText)
  53. {
  54. if (textBox == null)
  55. {
  56. throw new ArgumentNullException("textBox");
  57. }
  58. textBox.SetValue(DefaultTextAfterCommandExecutionProperty, defaultText);
  59. }
  60. /// <summary>
  61. /// Retrieves the default text set to the <see cref="TextBox"/> after the Command has been executed.
  62. /// </summary>
  63. /// <param name="textBox">TextBox dependency object on which the default text will be set after the Command has been executed.</param>
  64. /// <returns>Default text to set.</returns>
  65. public static string GetDefaultTextAfterCommandExecution(TextBox textBox)
  66. {
  67. if (textBox == null)
  68. {
  69. throw new ArgumentNullException("textBox");
  70. }
  71. return textBox.GetValue(DefaultTextAfterCommandExecutionProperty) as string;
  72. }
  73. /// <summary>
  74. /// Sets the <see cref="ICommand"/> to execute on the return key event.
  75. /// </summary>
  76. /// <param name="textBox">TextBox dependency object to attach command</param>
  77. /// <param name="command">Command to attach</param>
  78. public static void SetCommand(TextBox textBox, ICommand command)
  79. {
  80. if (textBox == null)
  81. {
  82. throw new ArgumentNullException("textBox");
  83. }
  84. textBox.SetValue(CommandProperty, command);
  85. }
  86. /// <summary>
  87. /// Retrieves the <see cref="ICommand"/> attached to the <see cref="TextBox"/>.
  88. /// </summary>
  89. /// <param name="textBox">TextBox containing the Command dependency property</param>
  90. /// <returns>The value of the command attached</returns>
  91. public static ICommand GetCommand(TextBox textBox)
  92. {
  93. if (textBox == null)
  94. {
  95. throw new ArgumentNullException("textBox");
  96. }
  97. return textBox.GetValue(CommandProperty) as ICommand;
  98. }
  99. private static void OnSetDefaultTextAfterCommandExecutionCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
  100. {
  101. TextBox textBox = dependencyObject as TextBox;
  102. if (textBox != null)
  103. {
  104. ReturnCommandBehavior behavior = GetOrCreateBehavior(textBox);
  105. behavior.DefaultTextAfterCommandExecution = e.NewValue as string;
  106. }
  107. }
  108. private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
  109. {
  110. TextBox textBox = dependencyObject as TextBox;
  111. if (textBox != null)
  112. {
  113. ReturnCommandBehavior behavior = GetOrCreateBehavior(textBox);
  114. behavior.Command = e.NewValue as ICommand;
  115. }
  116. }
  117. private static ReturnCommandBehavior GetOrCreateBehavior(TextBox textBox)
  118. {
  119. ReturnCommandBehavior behavior = textBox.GetValue(ReturnCommandBehaviorProperty) as ReturnCommandBehavior;
  120. if (behavior == null)
  121. {
  122. behavior = new ReturnCommandBehavior(textBox);
  123. textBox.SetValue(ReturnCommandBehaviorProperty, behavior);
  124. }
  125. return behavior;
  126. }
  127. }
  128. }