PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/CommandTextBox.cs

https://github.com/shader/QuickArch
C# | 163 lines | 143 code | 14 blank | 6 comment | 16 complexity | 1cccff45daaa4ba40922d6785a8f53fb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows;
  8. namespace QuickArch
  9. {
  10. public class CommandTextBox : TextBox, ICommandSource
  11. {
  12. #region Dependency Properties
  13. // Make Command a dependency property so it can use databinding.
  14. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
  15. "Command",
  16. typeof(ICommand),
  17. typeof(CommandTextBox),
  18. new PropertyMetadata((ICommand)null,
  19. new PropertyChangedCallback(CommandChanged)));
  20. public ICommand Command
  21. {
  22. get
  23. {
  24. return (ICommand)GetValue(CommandProperty);
  25. }
  26. set
  27. {
  28. SetValue(CommandProperty, value);
  29. }
  30. }
  31. public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register(
  32. "CommandTarget",
  33. typeof(IInputElement),
  34. typeof(CommandTextBox),
  35. new PropertyMetadata((IInputElement)null));
  36. public IInputElement CommandTarget
  37. {
  38. get
  39. {
  40. return (IInputElement)GetValue(CommandTargetProperty);
  41. }
  42. set
  43. {
  44. SetValue(CommandTargetProperty, value);
  45. }
  46. }
  47. public static readonly DependencyProperty CommandParameterProperty =
  48. DependencyProperty.Register(
  49. "CommandParameter",
  50. typeof(object),
  51. typeof(CommandTextBox),
  52. new PropertyMetadata((object)null));
  53. public object CommandParameter
  54. {
  55. get
  56. {
  57. return (object)GetValue(CommandParameterProperty);
  58. }
  59. set
  60. {
  61. SetValue(CommandParameterProperty, value);
  62. }
  63. }
  64. #endregion
  65. private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  66. {
  67. CommandTextBox cb = (CommandTextBox)d;
  68. cb.HookUpCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
  69. }
  70. private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
  71. {
  72. // If oldCommand is not null, then we need to remove the handlers.
  73. if (oldCommand != null)
  74. {
  75. RemoveCommand(oldCommand, newCommand);
  76. }
  77. AddCommand(oldCommand, newCommand);
  78. }
  79. private void RemoveCommand(ICommand oldCommand, ICommand newCommand)
  80. {
  81. EventHandler handler = CanExecuteChanged;
  82. oldCommand.CanExecuteChanged -= handler;
  83. }
  84. // Add the command.
  85. private void AddCommand(ICommand oldCommand, ICommand newCommand)
  86. {
  87. EventHandler handler = new EventHandler(CanExecuteChanged);
  88. canExecuteChangedHandler = handler;
  89. if (newCommand != null)
  90. {
  91. newCommand.CanExecuteChanged += canExecuteChangedHandler;
  92. }
  93. }
  94. private void CanExecuteChanged(object sender, EventArgs e)
  95. {
  96. if (this.Command != null)
  97. {
  98. RoutedCommand command = this.Command as RoutedCommand;
  99. // If a RoutedCommand.
  100. if (command != null)
  101. {
  102. if (command.CanExecute(CommandParameter, CommandTarget))
  103. {
  104. this.IsEnabled = true;
  105. }
  106. else
  107. {
  108. this.IsEnabled = false;
  109. }
  110. }
  111. // If a not RoutedCommand.
  112. else
  113. {
  114. if (Command.CanExecute(CommandParameter))
  115. {
  116. this.IsEnabled = true;
  117. }
  118. else
  119. {
  120. this.IsEnabled = false;
  121. }
  122. }
  123. }
  124. }
  125. //If Command is defined, hitting enter button will invoke the command
  126. protected override void OnKeyUp(KeyEventArgs e)
  127. {
  128. base.OnKeyUp(e);
  129. if (e.Key == Key.Enter)
  130. {
  131. if (this.Command != null)
  132. {
  133. RoutedCommand command = Command as RoutedCommand;
  134. if (command != null)
  135. {
  136. command.Execute(CommandParameter, CommandTarget);
  137. this.Clear();
  138. }
  139. else
  140. {
  141. ((ICommand)Command).Execute(CommandParameter);
  142. this.Clear();
  143. }
  144. }
  145. }
  146. }
  147. private static EventHandler canExecuteChangedHandler;
  148. }
  149. }