using MuchInfo.Chart.Infrastructure.Utilities; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; namespace MuchInfo.Chart.Infrastructure.Controls { /// /// BooleanEditor.xaml 的交互逻辑 /// public partial class BooleanEditor : UserControl { #region Fields private List _controlPairs; private string _propertyName; private object _target; #endregion Fields #region Constructors public BooleanEditor() { this.InitializeComponent(); } public BooleanEditor(object target, string propertyName, string label) : this(RuntimeHelpers.GetObjectValue(target), propertyName, label, null) { } public BooleanEditor(object target, string propertyName, string label, List enableTargets) { this.InitializeComponent(); this.SetUp(RuntimeHelpers.GetObjectValue(target), propertyName, label, enableTargets); } #endregion Constructors #region Properties #region Public Properties //private bool BooleanEditor_1230; public string Label { get { return this.chkItem.Content.ToString(); } set { this.chkItem.Content = (value); } } #endregion Public Properties #endregion Properties #region Methods #region Public Methods public void SetUp(object target, string propertyName, string label, List enableTargets) { this._controlPairs = enableTargets; this._target = RuntimeHelpers.GetObjectValue(target); this._propertyName = propertyName; this.Initialize(); this.chkItem.Content = label; } #endregion Public Methods #region Private Methods private void chkItem_Checked(object sender, RoutedEventArgs e) { this.SetPropertyValue(true); this.ShowControlPairs(); } private void chkItem_Unchecked(object sender, RoutedEventArgs e) { this.SetPropertyValue(false); this.ShowControlPairs(); } private void Initialize() { var type = this._target.GetType(); var property = type.GetProperty(this._propertyName); var objectValue = RuntimeHelpers.GetObjectValue(property.GetGetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), null)); var flag = (bool)objectValue; this.chkItem.IsChecked = flag; this.ShowControlPairs(); } private void SetPropertyValue(bool aVal) { var type = this._target.GetType(); var property = type.GetProperty(this._propertyName); var array = new object[] { aVal }; property.GetSetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array); } private void ShowControlPairs() { bool flag = this._controlPairs != null; if (flag) { foreach (var item in _controlPairs) { var isChecked = this.chkItem.IsChecked; item.SetVisible(isChecked != null && isChecked.Value); } } } #endregion Private Methods #endregion Methods } }