MuchinfoPagerTextBox.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. namespace Muchinfo.DataPager.Base
  8. {
  9. public class MuchinfoPagerTextBox : TextBox, ICommandSource //, IThemable
  10. {
  11. public static readonly DependencyProperty PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(MuchinfoPagerTextBox), new PropertyMetadata(-1, new PropertyChangedCallback(MuchinfoPagerTextBox.PageIndexChanged)));
  12. public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(MuchinfoPagerTextBox), new PropertyMetadata(null));
  13. public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MuchinfoPagerTextBox), new PropertyMetadata(new PropertyChangedCallback(MuchinfoPagerTextBox.OnCommandChanged)));
  14. public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(UIElement), typeof(MuchinfoPagerTextBox), null);
  15. public int PageIndex
  16. {
  17. get
  18. {
  19. return (int)base.GetValue(MuchinfoPagerTextBox.PageIndexProperty);
  20. }
  21. set
  22. {
  23. base.SetValue(MuchinfoPagerTextBox.PageIndexProperty, value);
  24. }
  25. }
  26. [TypeConverter(typeof(CommandConverter))]
  27. public ICommand Command
  28. {
  29. get
  30. {
  31. return (ICommand)base.GetValue(MuchinfoPagerTextBox.CommandProperty);
  32. }
  33. set
  34. {
  35. base.SetValue(MuchinfoPagerTextBox.CommandProperty, value);
  36. }
  37. }
  38. public object CommandParameter
  39. {
  40. get
  41. {
  42. return base.GetValue(MuchinfoPagerTextBox.CommandParameterProperty);
  43. }
  44. set
  45. {
  46. base.SetValue(MuchinfoPagerTextBox.CommandParameterProperty, value);
  47. }
  48. }
  49. public UIElement CommandTarget
  50. {
  51. get
  52. {
  53. return (UIElement)base.GetValue(MuchinfoPagerTextBox.CommandTargetProperty);
  54. }
  55. set
  56. {
  57. base.SetValue(MuchinfoPagerTextBox.CommandTargetProperty, value);
  58. }
  59. }
  60. IInputElement ICommandSource.CommandTarget
  61. {
  62. get
  63. {
  64. return null;
  65. }
  66. }
  67. private static void PageIndexChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  68. {
  69. ((MuchinfoPagerTextBox)sender).Text = ((int)args.NewValue + 1).ToString();
  70. }
  71. protected override void OnKeyDown(KeyEventArgs e)
  72. {
  73. base.OnKeyDown(e);
  74. if (e.Key == Key.Return)
  75. {
  76. this.RaiseCommand();
  77. }
  78. }
  79. private void RaiseCommand()
  80. {
  81. this.CommandParameter = this.ConvertTextToParameter();
  82. if (this.CommandParameter != null)
  83. {
  84. this.ExecuteCommand();
  85. }
  86. base.Text = (this.PageIndex + 1).ToString();
  87. }
  88. internal int ConvertTextToParameter()
  89. {
  90. int num;
  91. if (int.TryParse(base.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out num))
  92. {
  93. return num - 1;
  94. }
  95. return this.PageIndex;
  96. }
  97. protected override void OnLostFocus(RoutedEventArgs e)
  98. {
  99. base.OnLostFocus(e);
  100. this.RaiseCommand();
  101. }
  102. private void ExecuteCommand()
  103. {
  104. if (this.Command != null)
  105. {
  106. var routedCommand = this.Command as RoutedCommand;
  107. if (routedCommand == null)
  108. {
  109. this.Command.Execute(this.CommandParameter);
  110. return;
  111. }
  112. routedCommand.Execute(this.CommandParameter, this.CommandTarget ?? this);
  113. }
  114. }
  115. private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  116. {
  117. var dataPagerTextBox = d as MuchinfoPagerTextBox;
  118. if (dataPagerTextBox != null)
  119. {
  120. dataPagerTextBox.ChangeCommand((ICommand)e.OldValue, (ICommand)e.NewValue);
  121. }
  122. }
  123. private void ChangeCommand(ICommand oldCommand, ICommand newCommand)
  124. {
  125. if (oldCommand != null)
  126. {
  127. oldCommand.CanExecuteChanged -= new EventHandler(this.CanExecuteChanged);
  128. }
  129. if (newCommand != null)
  130. {
  131. newCommand.CanExecuteChanged += new EventHandler(this.CanExecuteChanged);
  132. }
  133. this.CanExecuteApply();
  134. }
  135. private void CanExecuteApply()
  136. {
  137. if (this.Command != null)
  138. {
  139. var routedCommand = this.Command as RoutedCommand;
  140. base.IsEnabled = ((routedCommand == null) ? this.Command.CanExecute(this.CommandParameter) : routedCommand.CanExecute(this.CommandParameter, this.CommandTarget ?? this));
  141. }
  142. }
  143. private void CanExecuteChanged(object sender, EventArgs e)
  144. {
  145. this.CanExecuteApply();
  146. }
  147. }
  148. }