ChartSingleUpDown.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using Xceed.Wpf.Toolkit;
  7. namespace MuchInfo.Chart.Infrastructure.Controls
  8. {
  9. /// <summary>
  10. /// SingleUpDown.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class ChartSingleUpDown : UserControl
  13. {
  14. #region Fields
  15. private CheckBox _checkBox;
  16. private string _checkPropertyName;
  17. private bool _flag;
  18. private int _propertyIndex;
  19. private string _propertyName;
  20. private SingleUpDown _spinner;
  21. private object _target;
  22. #endregion Fields
  23. #region Constructors
  24. public ChartSingleUpDown(object target, string propertyName)
  25. : this(RuntimeHelpers.GetObjectValue(target), propertyName, 1, 500, 0.1f)
  26. {
  27. }
  28. public ChartSingleUpDown(object target, string propertyName, int minVal, int maxVal, float increment)
  29. : this(RuntimeHelpers.GetObjectValue(target), propertyName, -1, null, minVal, maxVal, increment)
  30. {
  31. }
  32. public ChartSingleUpDown(object target, string propertyName, int propertyIndex, string checkPropertyName, int minVal, int maxVal, float increment)
  33. {
  34. this._spinner = new SingleUpDown
  35. {
  36. Maximum = (float)maxVal,
  37. Minimum = (float)minVal,
  38. Increment = increment,
  39. FormatString = "N0",
  40. Width = 70.0
  41. };
  42. //this._spinner.IntegersOnly = false;
  43. _spinner.ValueChanged += Spinner_ValueChanged;
  44. this._spinner.SetValue(Grid.ColumnProperty, 2);
  45. this._checkBox = new CheckBox();
  46. this._checkBox.Checked += CheckBox_Checked; ;
  47. this._checkBox.Unchecked += CheckBox_Unchecked;
  48. this._propertyIndex = -1;
  49. this._flag = true;
  50. this._target = RuntimeHelpers.GetObjectValue(target);
  51. this._propertyName = propertyName;
  52. this._checkPropertyName = checkPropertyName;
  53. this._propertyIndex = propertyIndex;
  54. this.InitializeComponent();
  55. this.LayoutRoot.Children.Add(this._spinner);
  56. this.SetSpinnerValue();
  57. if (!string.IsNullOrWhiteSpace(checkPropertyName))
  58. {
  59. this._checkBox.Margin = new Thickness(4.0, 0.0, 6.0, 0.0);
  60. this._checkBox.VerticalAlignment = (VerticalAlignment)(1);
  61. this._checkBox.SetValue(Grid.ColumnProperty, 1);
  62. this.LayoutRoot.Children.Add(this._checkBox);
  63. this.SetCheckBoxValue();
  64. }
  65. this._flag = false;
  66. }
  67. #endregion Constructors
  68. #region Methods
  69. #region Private Methods
  70. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  71. {
  72. bool singleUpDown_ = this._flag;
  73. if (!singleUpDown_)
  74. {
  75. this.SetCheckPropertyValue(true);
  76. }
  77. }
  78. private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
  79. {
  80. bool singleUpDown_ = this._flag;
  81. if (!singleUpDown_)
  82. {
  83. this.SetCheckPropertyValue(false);
  84. }
  85. }
  86. private void SetCheckBoxValue()
  87. {
  88. Type type = this._target.GetType();
  89. PropertyInfo property = type.GetProperty(this._checkPropertyName);
  90. object[] array = null;
  91. bool flag = this._propertyIndex != -1;
  92. if (flag)
  93. {
  94. array = new object[]
  95. {
  96. this._propertyIndex
  97. };
  98. }
  99. object objectValue = RuntimeHelpers.GetObjectValue(property.GetGetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array));
  100. bool flag2 = (bool)objectValue;
  101. this._checkBox.IsChecked = flag2;
  102. }
  103. private void SetCheckPropertyValue(bool aVal)
  104. {
  105. Type type = this._target.GetType();
  106. PropertyInfo property = type.GetProperty(this._checkPropertyName);
  107. bool flag = this._propertyIndex == -1;
  108. object[] array;
  109. if (flag)
  110. {
  111. array = new object[]
  112. {
  113. aVal
  114. };
  115. }
  116. else
  117. {
  118. array = new object[]
  119. {
  120. this._propertyIndex,
  121. aVal
  122. };
  123. }
  124. property.GetSetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array);
  125. }
  126. private void SetPropertyValue(float aVal)
  127. {
  128. Type type = this._target.GetType();
  129. PropertyInfo property = type.GetProperty(this._propertyName);
  130. bool flag = this._propertyIndex == -1;
  131. object[] array;
  132. if (flag)
  133. {
  134. array = new object[]
  135. {
  136. aVal
  137. };
  138. }
  139. else
  140. {
  141. array = new object[]
  142. {
  143. this._propertyIndex,
  144. aVal
  145. };
  146. }
  147. property.GetSetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array);
  148. }
  149. private void SetSpinnerValue()
  150. {
  151. Type type = this._target.GetType();
  152. PropertyInfo property = type.GetProperty(this._propertyName);
  153. object[] array = null;
  154. bool flag = this._propertyIndex != -1;
  155. if (flag)
  156. {
  157. array = new object[]
  158. {
  159. this._propertyIndex
  160. };
  161. }
  162. object objectValue = RuntimeHelpers.GetObjectValue(property.GetGetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array));
  163. float singleValue = (float)objectValue;
  164. this._spinner.Value = singleValue;
  165. }
  166. void Spinner_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  167. {
  168. if (!_flag)
  169. {
  170. this.SetPropertyValue(this._spinner.Value.HasValue ? this._spinner.Value.Value :
  171. (this._spinner.Minimum.HasValue ? this._spinner.Minimum.Value : 1));
  172. OnValueChanged();
  173. }
  174. }
  175. #endregion Private Methods
  176. #endregion Methods
  177. public event EventHandler ValueChanged;
  178. protected virtual void OnValueChanged()
  179. {
  180. var handler = ValueChanged;
  181. if (handler != null) handler(this, System.EventArgs.Empty);
  182. }
  183. }
  184. }