TextBoxEx.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. }
  67. }