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 { /// /// SingleUpDown.xaml 的交互逻辑 /// 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 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); } } }