DatePicker.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Windows;
  2. using System.Windows.Controls.Primitives;
  3. using System.Windows.Data;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. using HandyControl.Interactivity;
  7. namespace HandyControl.Controls;
  8. [TemplatePart(Name = ElementTextBox, Type = typeof(DatePickerTextBox))]
  9. public class DatePicker : System.Windows.Controls.DatePicker
  10. {
  11. private const string ElementTextBox = "PART_TextBox";
  12. private System.Windows.Controls.TextBox _textBox;
  13. public DatePicker()
  14. {
  15. CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) =>
  16. {
  17. SetCurrentValue(SelectedDateProperty, null);
  18. SetCurrentValue(TextProperty, "");
  19. }));
  20. }
  21. public override void OnApplyTemplate()
  22. {
  23. base.OnApplyTemplate();
  24. _textBox = GetTemplateChild(ElementTextBox) as System.Windows.Controls.TextBox;
  25. if (_textBox != null)
  26. {
  27. _textBox.SetBinding(SelectionBrushProperty, new Binding(SelectionBrushProperty.Name) { Source = this });
  28. #if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472)
  29. _textBox.SetBinding(SelectionTextBrushProperty, new Binding(SelectionTextBrushProperty.Name) { Source = this });
  30. #endif
  31. _textBox.SetBinding(SelectionOpacityProperty, new Binding(SelectionOpacityProperty.Name) { Source = this });
  32. _textBox.SetBinding(CaretBrushProperty, new Binding(CaretBrushProperty.Name) { Source = this });
  33. }
  34. }
  35. public static readonly DependencyProperty SelectionBrushProperty =
  36. TextBoxBase.SelectionBrushProperty.AddOwner(typeof(DatePicker));
  37. public Brush SelectionBrush
  38. {
  39. get => (Brush) GetValue(SelectionBrushProperty);
  40. set => SetValue(SelectionBrushProperty, value);
  41. }
  42. #if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472)
  43. public static readonly DependencyProperty SelectionTextBrushProperty =
  44. TextBoxBase.SelectionTextBrushProperty.AddOwner(typeof(DatePicker));
  45. public Brush SelectionTextBrush
  46. {
  47. get => (Brush) GetValue(SelectionTextBrushProperty);
  48. set => SetValue(SelectionTextBrushProperty, value);
  49. }
  50. #endif
  51. public static readonly DependencyProperty SelectionOpacityProperty =
  52. TextBoxBase.SelectionOpacityProperty.AddOwner(typeof(DatePicker));
  53. public double SelectionOpacity
  54. {
  55. get => (double) GetValue(SelectionOpacityProperty);
  56. set => SetValue(SelectionOpacityProperty, value);
  57. }
  58. public static readonly DependencyProperty CaretBrushProperty =
  59. TextBoxBase.CaretBrushProperty.AddOwner(typeof(DatePicker));
  60. public Brush CaretBrush
  61. {
  62. get => (Brush) GetValue(CaretBrushProperty);
  63. set => SetValue(CaretBrushProperty, value);
  64. }
  65. }