ControlAttachPropertyHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. //using System.Windows.Forms;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Animation;
  8. using CheckBox = System.Windows.Controls.CheckBox;
  9. using ComboBox = System.Windows.Controls.ComboBox;
  10. using OpenFileDialog = Microsoft.Win32.OpenFileDialog;
  11. using RadioButton = System.Windows.Controls.RadioButton;
  12. using RichTextBox = System.Windows.Controls.RichTextBox;
  13. using TextBox = System.Windows.Controls.TextBox;
  14. namespace Muchinfo.MTPClient.Infrastructure.Helpers
  15. {
  16. /// <summary>
  17. /// 公共附加属性
  18. /// </summary>
  19. public static class ControlAttachPropertyHelper
  20. {
  21. /************************************ Attach Property **************************************/
  22. #region WatermarkProperty 水印
  23. /// <summary>
  24. /// 水印
  25. /// </summary>
  26. public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached(
  27. "Watermark", typeof(string), typeof(ControlAttachPropertyHelper), new FrameworkPropertyMetadata(""));
  28. public static string GetWatermark(DependencyObject d)
  29. {
  30. return (string)d.GetValue(WatermarkProperty);
  31. }
  32. public static void SetWatermark(DependencyObject obj, string value)
  33. {
  34. obj.SetValue(WatermarkProperty, value);
  35. }
  36. #endregion
  37. /************************************ RoutedUICommand Behavior enable **************************************/
  38. #region IsClearTextButtonBehaviorEnabledProperty 清除输入框Text值按钮行为开关(设为ture时才会绑定事件)
  39. /// <summary>
  40. /// 清除输入框Text值按钮行为开关
  41. /// </summary>
  42. public static readonly DependencyProperty IsClearTextButtonBehaviorEnabledProperty = DependencyProperty.RegisterAttached("IsClearTextButtonBehaviorEnabled"
  43. , typeof(bool), typeof(ControlAttachPropertyHelper), new FrameworkPropertyMetadata(false, IsClearTextButtonBehaviorEnabledChanged));
  44. [AttachedPropertyBrowsableForType(typeof(TextBox))]
  45. public static bool GetIsClearTextButtonBehaviorEnabled(DependencyObject d)
  46. {
  47. return (bool)d.GetValue(IsClearTextButtonBehaviorEnabledProperty);
  48. }
  49. public static void SetIsClearTextButtonBehaviorEnabled(DependencyObject obj, bool value)
  50. {
  51. obj.SetValue(IsClearTextButtonBehaviorEnabledProperty, value);
  52. }
  53. /// <summary>
  54. /// 绑定清除Text操作的按钮事件
  55. /// </summary>
  56. private static void IsClearTextButtonBehaviorEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  57. {
  58. //var button = d as FButton;
  59. //if (e.OldValue != e.NewValue && button != null)
  60. //{
  61. // button.CommandBindings.Add(ClearTextCommandBinding);
  62. //}
  63. }
  64. #endregion
  65. /************************************ RoutedUICommand **************************************/
  66. #region ClearTextCommand 清除输入框Text事件命令
  67. /// <summary>
  68. /// 清除输入框Text事件命令,需要使用IsClearTextButtonBehaviorEnabledChanged绑定命令
  69. /// </summary>
  70. public static RoutedUICommand ClearTextCommand { get; private set; }
  71. /// <summary>
  72. /// ClearTextCommand绑定事件
  73. /// </summary>
  74. private static readonly CommandBinding ClearTextCommandBinding;
  75. /// <summary>
  76. /// 清除输入框文本值
  77. /// </summary>
  78. private static void ClearButtonClick(object sender, ExecutedRoutedEventArgs e)
  79. {
  80. var tbox = e.Parameter as FrameworkElement;
  81. if (tbox == null) return;
  82. if (tbox is TextBox)
  83. {
  84. ((TextBox)tbox).Clear();
  85. }
  86. //if (tbox is PasswordBox)
  87. //{
  88. // ((PasswordBox)tbox).Clear();
  89. //}
  90. //if (tbox is ComboBox)
  91. //{
  92. // var cb = tbox as ComboBox;
  93. // cb.SelectedItem = null;
  94. // cb.Text = string.Empty;
  95. //}
  96. //if (tbox is MultiComboBox)
  97. //{
  98. // var cb = tbox as MultiComboBox;
  99. // cb.SelectedItem = null;
  100. // cb.UnselectAll();
  101. // cb.Text = string.Empty;
  102. //}
  103. //if (tbox is DatePicker)
  104. //{
  105. // var dp = tbox as DatePicker;
  106. // dp.SelectedDate = null;
  107. // dp.Text = string.Empty;
  108. //}
  109. tbox.Focus();
  110. }
  111. #endregion
  112. /// <summary>
  113. /// 静态构造函数
  114. /// </summary>
  115. static ControlAttachPropertyHelper()
  116. {
  117. //ClearTextCommand
  118. ClearTextCommand = new RoutedUICommand();
  119. ClearTextCommandBinding = new CommandBinding(ClearTextCommand);
  120. ClearTextCommandBinding.Executed += ClearButtonClick;
  121. //OpenFileCommand
  122. //OpenFileCommand = new RoutedUICommand();
  123. //OpenFileCommandBinding = new CommandBinding(OpenFileCommand);
  124. //OpenFileCommandBinding.Executed += OpenFileButtonClick;
  125. ////OpenFolderCommand
  126. //OpenFolderCommand = new RoutedUICommand();
  127. //OpenFolderCommandBinding = new CommandBinding(OpenFolderCommand);
  128. //OpenFolderCommandBinding.Executed += OpenFolderButtonClick;
  129. //SaveFileCommand = new RoutedUICommand();
  130. //SaveFileCommandBinding = new CommandBinding(SaveFileCommand);
  131. //SaveFileCommandBinding.Executed += SaveFileButtonClick;
  132. }
  133. }
  134. }