TextBoxEx.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. namespace PDF_Office.CustomControl
  10. {
  11. /// <summary>
  12. /// 带预文本和圆角的textbox
  13. /// </summary>
  14. public class TextBoxEx : TextBox
  15. {
  16. static TextBoxEx()
  17. {
  18. DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx)));
  19. }
  20. public override void OnApplyTemplate()
  21. {
  22. //需要配合特定的Style才可用
  23. var btn = base.GetTemplateChild("PART_BtnClear") as Button;
  24. if(btn!=null)
  25. {
  26. WeakEventManager<Button, RoutedEventArgs>.AddHandler(btn, "Click", Btn_Click);
  27. }
  28. base.OnApplyTemplate();
  29. }
  30. private void Btn_Click(object sender, RoutedEventArgs e)
  31. {
  32. this.Text = "";
  33. this.Focus();
  34. }
  35. public string PlaceholderText
  36. {
  37. get { return (string)GetValue(PlaceholderTextProperty); }
  38. set { SetValue(PlaceholderTextProperty, value); }
  39. }
  40. public static readonly DependencyProperty PlaceholderTextProperty
  41. = DependencyProperty.Register("PlaceholderText", typeof(string), typeof(TextBoxEx), null);
  42. public CornerRadius CornerRadius
  43. {
  44. get { return (CornerRadius)GetValue(CornerRadiusProperty); }
  45. set { SetValue(CornerRadiusProperty, value); }
  46. }
  47. public static readonly DependencyProperty CornerRadiusProperty =
  48. DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TextBoxEx), new PropertyMetadata(new CornerRadius(4)));
  49. public Brush PlaceholderForeground
  50. {
  51. get { return (Brush)GetValue(PlaceholderForegroundProperty); }
  52. set { SetValue(PlaceholderForegroundProperty, value); }
  53. }
  54. public static readonly DependencyProperty PlaceholderForegroundProperty =
  55. DependencyProperty.Register("PlaceholderForeground", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#94989C"))));
  56. public Brush MouseOverForeground
  57. {
  58. get
  59. {
  60. return (Brush)GetValue(MouseOverForegroundProperty);
  61. }
  62. set { SetValue(MouseOverForegroundProperty, value); }
  63. }
  64. public static readonly DependencyProperty MouseOverForegroundProperty =
  65. DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
  66. public bool IsError
  67. {
  68. get { return (bool)GetValue(IsErrorProperty); }
  69. set { SetValue(IsErrorProperty, value); }
  70. }
  71. // Using a DependencyProperty as the backing store for IsError. This enables animation, styling, binding, etc...
  72. public static readonly DependencyProperty IsErrorProperty =
  73. DependencyProperty.Register("IsError", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(false));
  74. public Visibility ShowTipText
  75. {
  76. get { return (Visibility)GetValue(ShowTipTextProperty); }
  77. set { SetValue(ShowTipTextProperty, value); }
  78. }
  79. // Using a DependencyProperty as the backing store for ShowTipText. This enables animation, styling, binding, etc...
  80. public static readonly DependencyProperty ShowTipTextProperty =
  81. DependencyProperty.Register("ShowTipText", typeof(Visibility), typeof(TextBoxEx), new PropertyMetadata(Visibility.Collapsed));
  82. public bool ShowTip
  83. {
  84. get { return (bool)GetValue(ShowTipProperty); }
  85. set { SetValue(ShowTipProperty, value); }
  86. }
  87. // Using a DependencyProperty as the backing store for ShowTip. This enables animation, styling, binding, etc...
  88. public static readonly DependencyProperty ShowTipProperty =
  89. DependencyProperty.Register("ShowTip", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(false));
  90. public string TipText
  91. {
  92. get { return (string)GetValue(TipTextProperty); }
  93. set { SetValue(TipTextProperty, value); }
  94. }
  95. // Using a DependencyProperty as the backing store for TipText. This enables animation, styling, binding, etc...
  96. public static readonly DependencyProperty TipTextProperty =
  97. DependencyProperty.Register("TipText", typeof(string), typeof(TextBoxEx), new PropertyMetadata(""));
  98. }
  99. }