| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using System;
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Windows;
- using System.Windows.Controls;
- using Xceed.Wpf.Toolkit;
- namespace MuchInfo.Chart.Infrastructure.Controls
- {
- /// <summary>
- /// SingleUpDown.xaml 的交互逻辑
- /// </summary>
- public partial class ChartSingleUpDown : UserControl
- {
- #region Fields
- private CheckBox _checkBox;
- private string _checkPropertyName;
- private bool _flag;
- private int _propertyIndex;
- private string _propertyName;
- private SingleUpDown _spinner;
- private object _target;
- #endregion Fields
- #region Constructors
- public ChartSingleUpDown(object target, string propertyName)
- : this(RuntimeHelpers.GetObjectValue(target), propertyName, 1, 500, 0.1f)
- {
- }
- public ChartSingleUpDown(object target, string propertyName, int minVal, int maxVal, float increment)
- : this(RuntimeHelpers.GetObjectValue(target), propertyName, -1, null, minVal, maxVal, increment)
- {
- }
- public ChartSingleUpDown(object target, string propertyName, int propertyIndex, string checkPropertyName, int minVal, int maxVal, float increment)
- {
- this._spinner = new SingleUpDown
- {
- Maximum = (float)maxVal,
- Minimum = (float)minVal,
- Increment = increment,
- FormatString = "N0",
- Width = 70.0
- };
- //this._spinner.IntegersOnly = false;
- _spinner.ValueChanged += Spinner_ValueChanged;
- this._spinner.SetValue(Grid.ColumnProperty, 2);
- this._checkBox = new CheckBox();
- this._checkBox.Checked += CheckBox_Checked; ;
- this._checkBox.Unchecked += CheckBox_Unchecked;
- this._propertyIndex = -1;
- this._flag = true;
- this._target = RuntimeHelpers.GetObjectValue(target);
- this._propertyName = propertyName;
- this._checkPropertyName = checkPropertyName;
- this._propertyIndex = propertyIndex;
- this.InitializeComponent();
- this.LayoutRoot.Children.Add(this._spinner);
- this.SetSpinnerValue();
- if (!string.IsNullOrWhiteSpace(checkPropertyName))
- {
- this._checkBox.Margin = new Thickness(4.0, 0.0, 6.0, 0.0);
- this._checkBox.VerticalAlignment = (VerticalAlignment)(1);
- this._checkBox.SetValue(Grid.ColumnProperty, 1);
- this.LayoutRoot.Children.Add(this._checkBox);
- this.SetCheckBoxValue();
- }
- this._flag = false;
- }
- #endregion Constructors
- #region Methods
- #region Private Methods
- private void CheckBox_Checked(object sender, RoutedEventArgs e)
- {
- bool singleUpDown_ = this._flag;
- if (!singleUpDown_)
- {
- this.SetCheckPropertyValue(true);
- }
- }
- private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
- {
- bool singleUpDown_ = this._flag;
- if (!singleUpDown_)
- {
- this.SetCheckPropertyValue(false);
- }
- }
- private void SetCheckBoxValue()
- {
- Type type = this._target.GetType();
- PropertyInfo property = type.GetProperty(this._checkPropertyName);
- object[] array = null;
- bool flag = this._propertyIndex != -1;
- if (flag)
- {
- array = new object[]
- {
- this._propertyIndex
- };
- }
- object objectValue = RuntimeHelpers.GetObjectValue(property.GetGetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array));
- bool flag2 = (bool)objectValue;
- this._checkBox.IsChecked = flag2;
- }
- private void SetCheckPropertyValue(bool aVal)
- {
- Type type = this._target.GetType();
- PropertyInfo property = type.GetProperty(this._checkPropertyName);
- bool flag = this._propertyIndex == -1;
- object[] array;
- if (flag)
- {
- array = new object[]
- {
- aVal
- };
- }
- else
- {
- array = new object[]
- {
- this._propertyIndex,
- aVal
- };
- }
- property.GetSetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array);
- }
- private void SetPropertyValue(float aVal)
- {
- Type type = this._target.GetType();
- PropertyInfo property = type.GetProperty(this._propertyName);
- bool flag = this._propertyIndex == -1;
- object[] array;
- if (flag)
- {
- array = new object[]
- {
- aVal
- };
- }
- else
- {
- array = new object[]
- {
- this._propertyIndex,
- aVal
- };
- }
- property.GetSetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array);
- }
- private void SetSpinnerValue()
- {
- Type type = this._target.GetType();
- PropertyInfo property = type.GetProperty(this._propertyName);
- object[] array = null;
- bool flag = this._propertyIndex != -1;
- if (flag)
- {
- array = new object[]
- {
- this._propertyIndex
- };
- }
- object objectValue = RuntimeHelpers.GetObjectValue(property.GetGetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array));
- float singleValue = (float)objectValue;
- this._spinner.Value = singleValue;
- }
- void Spinner_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
- {
- if (!_flag)
- {
- this.SetPropertyValue(this._spinner.Value.HasValue ? this._spinner.Value.Value :
- (this._spinner.Minimum.HasValue ? this._spinner.Minimum.Value : 1));
- OnValueChanged();
- }
- }
- #endregion Private Methods
- #endregion Methods
- public event EventHandler ValueChanged;
- protected virtual void OnValueChanged()
- {
- var handler = ValueChanged;
- if (handler != null) handler(this, System.EventArgs.Empty);
- }
- }
- }
|