myNumericUpDowm.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace TeamAAS_VP.Controls
  17. {
  18. /// <summary>
  19. /// myNumericUpDowm.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class myNumericUpDowm : UserControl, INotifyPropertyChanged
  22. {
  23. //
  24. // 摘要:
  25. // Identifies the MahApps.Metro.Controls.NumericUpDown.ValueChanged routed event.
  26. public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
  27. "ValueChanged",
  28. RoutingStrategy.Bubble,
  29. typeof(RoutedEventHandler),
  30. typeof(myNumericUpDowm));
  31. //
  32. // 摘要:
  33. // Add / Remove ValueChangedEvent handler Event which will be fired from this NumericUpDown
  34. // when its value has been changed.
  35. public event RoutedEventHandler ValueChanged
  36. {
  37. add
  38. {
  39. AddHandler(ValueChangedEvent, value);
  40. }
  41. remove
  42. {
  43. RemoveHandler(ValueChangedEvent, value);
  44. }
  45. }
  46. public myNumericUpDowm()
  47. {
  48. InitializeComponent();
  49. this.MotorAmplitudeNumeric1.ValueChanged += MotorAmplitudeNumeric1_ValueChanged;
  50. }
  51. private void MotorAmplitudeNumeric1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double?> e)
  52. {
  53. Value = e.NewValue;
  54. }
  55. [Bindable(true)]
  56. [Category("Common")]
  57. [DefaultValue(null)]
  58. public double? Value
  59. {
  60. get { return (double?)GetValue(ValueProperty); }
  61. set { SetValue(ValueProperty, value); NotifyValueChanged(value); }
  62. }
  63. // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc...
  64. public static readonly DependencyProperty ValueProperty =
  65. DependencyProperty.Register("Value", typeof(double?), typeof(myNumericUpDowm), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(PropertyValueChanged)));
  66. [Bindable(true)]
  67. [Category("Common")]
  68. [DefaultValue("")]
  69. public string StringFormat
  70. {
  71. get { return (string)GetValue(StringFormatProperty); }
  72. set { SetValue(StringFormatProperty, value); }
  73. }
  74. // Using a DependencyProperty as the backing store for StringFormat. This enables animation, styling, binding, etc...
  75. public static readonly DependencyProperty StringFormatProperty =
  76. DependencyProperty.Register("StringFormat", typeof(string), typeof(myNumericUpDowm), new PropertyMetadata("", new PropertyChangedCallback(PropertyValueChanged)));
  77. [Bindable(true)]
  78. [Category("Common")]
  79. [DefaultValue(double.MinValue)]
  80. public double Minimum
  81. {
  82. get { return (double)GetValue(MinimumProperty); }
  83. set { SetValue(MinimumProperty, value); }
  84. }
  85. // Using a DependencyProperty as the backing store for Minimum. This enables animation, styling, binding, etc...
  86. public static readonly DependencyProperty MinimumProperty =
  87. DependencyProperty.Register("Minimum", typeof(double), typeof(myNumericUpDowm), new PropertyMetadata(double.MinValue, new PropertyChangedCallback(PropertyValueChanged)));
  88. [Bindable(true)]
  89. [Category("Common")]
  90. [DefaultValue(double.MaxValue)]
  91. public double Maximum
  92. {
  93. get { return (double)GetValue(MaximumProperty); }
  94. set { SetValue(MaximumProperty, value); }
  95. }
  96. // Using a DependencyProperty as the backing store for Maximum. This enables animation, styling, binding, etc...
  97. public static readonly DependencyProperty MaximumProperty =
  98. DependencyProperty.Register("Maximum", typeof(double), typeof(myNumericUpDowm), new PropertyMetadata(double.MaxValue, new PropertyChangedCallback(PropertyValueChanged)));
  99. [Bindable(true)]
  100. [Category("Common")]
  101. [DefaultValue(1.0)]
  102. public double Interval
  103. {
  104. get { return (double)GetValue(IntervalProperty); }
  105. set { SetValue(IntervalProperty, value); }
  106. }
  107. // Using a DependencyProperty as the backing store for Interval. This enables animation, styling, binding, etc...
  108. public static readonly DependencyProperty IntervalProperty =
  109. DependencyProperty.Register("Interval", typeof(double), typeof(myNumericUpDowm), new PropertyMetadata(1.0, new PropertyChangedCallback(PropertyValueChanged)));
  110. public event PropertyChangedEventHandler PropertyChanged;
  111. private static void PropertyValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  112. {
  113. (d as myNumericUpDowm).Update();
  114. }
  115. private void Update()
  116. {
  117. this.MotorAmplitudeNumeric1.Value = Value;
  118. this.MotorAmplitudeNumeric1.StringFormat = StringFormat;
  119. this.MotorAmplitudeNumeric1.Minimum = Minimum;
  120. this.MotorAmplitudeNumeric1.Maximum = Maximum;
  121. this.MotorAmplitudeNumeric1.Interval = Interval;
  122. this.slider2.SmallChange = Interval;
  123. this.slider2.TickFrequency= Interval;
  124. }
  125. // 触发 ValueChanged 事件的方法
  126. protected virtual void OnValueChanged(double? newValue)
  127. {
  128. RoutedEventArgs args = new RoutedEventArgs(ValueChangedEvent, newValue);
  129. RaiseEvent(args);
  130. }
  131. // 用户控件的值发生变化时调用此方法
  132. private void NotifyValueChanged(double? newValue)
  133. {
  134. OnValueChanged(newValue);
  135. }
  136. }
  137. }