BooleanEditor.xaml.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using MuchInfo.Chart.Infrastructure.Utilities;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace MuchInfo.Chart.Infrastructure.Controls
  7. {
  8. /// <summary>
  9. /// BooleanEditor.xaml 的交互逻辑
  10. /// </summary>
  11. public partial class BooleanEditor : UserControl
  12. {
  13. #region Fields
  14. private List<ControlPair> _controlPairs;
  15. private string _propertyName;
  16. private object _target;
  17. #endregion Fields
  18. #region Constructors
  19. public BooleanEditor()
  20. {
  21. this.InitializeComponent();
  22. }
  23. public BooleanEditor(object target, string propertyName, string label)
  24. : this(RuntimeHelpers.GetObjectValue(target), propertyName, label, null)
  25. {
  26. }
  27. public BooleanEditor(object target, string propertyName, string label, List<ControlPair> enableTargets)
  28. {
  29. this.InitializeComponent();
  30. this.SetUp(RuntimeHelpers.GetObjectValue(target), propertyName, label, enableTargets);
  31. }
  32. #endregion Constructors
  33. #region Properties
  34. #region Public Properties
  35. //private bool BooleanEditor_1230;
  36. public string Label
  37. {
  38. get
  39. {
  40. return this.chkItem.Content.ToString();
  41. }
  42. set
  43. {
  44. this.chkItem.Content = (value);
  45. }
  46. }
  47. #endregion Public Properties
  48. #endregion Properties
  49. #region Methods
  50. #region Public Methods
  51. public void SetUp(object target, string propertyName, string label, List<ControlPair> enableTargets)
  52. {
  53. this._controlPairs = enableTargets;
  54. this._target = RuntimeHelpers.GetObjectValue(target);
  55. this._propertyName = propertyName;
  56. this.Initialize();
  57. this.chkItem.Content = label;
  58. }
  59. #endregion Public Methods
  60. #region Private Methods
  61. private void chkItem_Checked(object sender, RoutedEventArgs e)
  62. {
  63. this.SetPropertyValue(true);
  64. this.ShowControlPairs();
  65. }
  66. private void chkItem_Unchecked(object sender, RoutedEventArgs e)
  67. {
  68. this.SetPropertyValue(false);
  69. this.ShowControlPairs();
  70. }
  71. private void Initialize()
  72. {
  73. var type = this._target.GetType();
  74. var property = type.GetProperty(this._propertyName);
  75. var objectValue = RuntimeHelpers.GetObjectValue(property.GetGetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), null));
  76. var flag = (bool)objectValue;
  77. this.chkItem.IsChecked = flag;
  78. this.ShowControlPairs();
  79. }
  80. private void SetPropertyValue(bool aVal)
  81. {
  82. var type = this._target.GetType();
  83. var property = type.GetProperty(this._propertyName);
  84. var array = new object[]
  85. {
  86. aVal
  87. };
  88. property.GetSetMethod().Invoke(RuntimeHelpers.GetObjectValue(this._target), array);
  89. }
  90. private void ShowControlPairs()
  91. {
  92. bool flag = this._controlPairs != null;
  93. if (flag)
  94. {
  95. foreach (var item in _controlPairs)
  96. {
  97. var isChecked = this.chkItem.IsChecked;
  98. item.SetVisible(isChecked != null && isChecked.Value);
  99. }
  100. }
  101. }
  102. #endregion Private Methods
  103. #endregion Methods
  104. }
  105. }