NumericUpDown.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace CustomControls
  6. {
  7. [DesignTimeVisible(false)]
  8. public partial class NumericUpDown : UserControl, INotifyPropertyChanged
  9. {
  10. public NumericUpDown()
  11. {
  12. InitializeComponent();
  13. }
  14. public event PropertyChangedEventHandler PropertyChanged;
  15. public decimal Maximum
  16. {
  17. get { return (decimal)GetValue(MaximumProperty); }
  18. set { SetValue(MaximumProperty, value); }
  19. }
  20. public readonly static DependencyProperty MaximumProperty = DependencyProperty.Register(
  21. "Maximum", typeof(decimal), typeof(NumericUpDown), new UIPropertyMetadata(100m));
  22. public decimal Minimum
  23. {
  24. get { return (decimal)GetValue(MinimumProperty); }
  25. set { SetValue(MinimumProperty, value); }
  26. }
  27. public readonly static DependencyProperty MinimumProperty = DependencyProperty.Register(
  28. "Minimum", typeof(decimal), typeof(NumericUpDown), new UIPropertyMetadata(0m));
  29. public decimal Value
  30. {
  31. get { return (decimal)GetValue(ValueProperty); }
  32. set { SetCurrentValue(ValueProperty, value); }
  33. }
  34. public readonly static DependencyProperty ValueProperty = DependencyProperty.Register(
  35. "Value", typeof(decimal), typeof(NumericUpDown), new UIPropertyMetadata(0m, (o, e) =>
  36. {
  37. NumericUpDown tb = (NumericUpDown)o;
  38. tb.RaiseValueChangedEvent(e);
  39. }));
  40. public event EventHandler<DependencyPropertyChangedEventArgs> ValueChanged;
  41. private void RaiseValueChangedEvent(DependencyPropertyChangedEventArgs e)
  42. {
  43. ValueChanged?.Invoke(this, e);
  44. }
  45. public decimal Increment
  46. {
  47. get { return (decimal)GetValue(IncrementProperty); }
  48. set { SetValue(IncrementProperty, value); }
  49. }
  50. public readonly static DependencyProperty IncrementProperty = DependencyProperty.Register(
  51. "Increment", typeof(decimal), typeof(NumericUpDown), new UIPropertyMetadata(1m));
  52. public int DecimalPlaces
  53. {
  54. get { return (int)GetValue(DecimalPlacesProperty); }
  55. set { SetValue(DecimalPlacesProperty, value); }
  56. }
  57. public static readonly DependencyProperty DecimalPlacesProperty = DependencyProperty.Register(
  58. "DecimalPlaces", typeof(int), typeof(NumericUpDown), new PropertyMetadata(0));
  59. public string FormattedValue
  60. {
  61. get => Value.ToString("N" + DecimalPlaces.ToString());
  62. set
  63. {
  64. if (decimal.TryParse(value, out decimal d))
  65. Value = d;
  66. }
  67. }
  68. protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
  69. {
  70. base.OnPropertyChanged(e);
  71. if (e.Property.Name == "Value" || e.Property.Name == "DecimalPlaces")
  72. {
  73. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FormattedValue"));
  74. }
  75. }
  76. private void btnUp_Click(object sender, RoutedEventArgs e)
  77. {
  78. if (Value < Maximum)
  79. {
  80. Value += Increment;
  81. if (Value > Maximum)
  82. Value = Maximum;
  83. }
  84. }
  85. private void btnDown_Click(object sender, RoutedEventArgs e)
  86. {
  87. if (Value > Minimum)
  88. {
  89. Value -= Increment;
  90. if (Value < Minimum)
  91. Value = Minimum;
  92. }
  93. }
  94. private void tbmain_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  95. {
  96. if (e.Key == System.Windows.Input.Key.Enter)
  97. {
  98. ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
  99. }
  100. }
  101. }
  102. }