ButtonOptions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. namespace FWindSoft.Wpf.Controls
  6. {
  7. public class ButtonOptions
  8. {
  9. public static readonly DependencyProperty IconProperty =
  10. DependencyProperty.RegisterAttached("Icon", typeof(ImageSource), typeof(ButtonOptions), new PropertyMetadata(null, OnShowChanged));
  11. [AttachedPropertyBrowsableForType(typeof(Button))]
  12. public static ImageSource GetIcon(DependencyObject dp)
  13. {
  14. return dp.GetValue(IconProperty) as ImageSource;
  15. }
  16. [AttachedPropertyBrowsableForType(typeof(Button))]
  17. public static void SetIcon(DependencyObject dp, object value)
  18. {
  19. dp.SetValue(IconProperty,value);
  20. }
  21. static ButtonOptions()
  22. {
  23. }
  24. public static void OnShowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  25. {
  26. TextBox textBox = d as TextBox;
  27. if (textBox == null)
  28. return;
  29. }
  30. #region 子对象CheckBox控制
  31. public static readonly DependencyProperty SelectTypeProperty =
  32. DependencyProperty.RegisterAttached("SelectType", typeof(SelectType), typeof(ButtonOptions), new PropertyMetadata(SelectType.UnDefine, OnSelectTypeChanged));
  33. [AttachedPropertyBrowsableForType(typeof(Button))]
  34. public static SelectType GetSelectType(DependencyObject dp)
  35. {
  36. return (SelectType)dp.GetValue(SelectTypeProperty);
  37. }
  38. [AttachedPropertyBrowsableForType(typeof(Button))]
  39. public static void SetSelectType(DependencyObject dp, SelectType value)
  40. {
  41. dp.SetValue(SelectTypeProperty, value);
  42. }
  43. public static readonly DependencyProperty CheckBoxContainerProperty =
  44. DependencyProperty.RegisterAttached("CheckBoxContainer", typeof(UIElement), typeof(ButtonOptions), new PropertyMetadata(null));
  45. [AttachedPropertyBrowsableForType(typeof(Button))]
  46. public static UIElement GetCheckBoxContainer(DependencyObject dp)
  47. {
  48. return (UIElement)dp.GetValue(CheckBoxContainerProperty);
  49. }
  50. [AttachedPropertyBrowsableForType(typeof(Button))]
  51. public static void SetCheckBoxContainer(DependencyObject dp, UIElement value)
  52. {
  53. dp.SetValue(CheckBoxContainerProperty, value);
  54. }
  55. public static void OnSelectTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  56. {
  57. Button button = d as Button;
  58. if (button == null)
  59. return;
  60. var selectType = (SelectType)e.NewValue;
  61. button.Click -= Button_Click;
  62. if(selectType!= SelectType.UnDefine)
  63. {
  64. button.Click += Button_Click;
  65. }
  66. }
  67. private static void Button_Click(object sender, RoutedEventArgs e)
  68. {
  69. Button button = sender as Button;
  70. if (button == null)
  71. return;
  72. var container = GetCheckBoxContainer(button);
  73. if (container == null)
  74. return;
  75. var checkBoxes = container.GetSpecifyTypeChildren<CheckBox>();
  76. var selectType = GetSelectType(button);
  77. if (selectType == SelectType.UnDefine)
  78. return;
  79. Func<CheckBox,bool> calcValue = (c) => false;
  80. if (selectType == SelectType.All)
  81. {
  82. calcValue=(c) => true;
  83. }
  84. else if (selectType == SelectType.Inverse)
  85. {
  86. calcValue = (c) => !(c.IsChecked == true);
  87. }
  88. foreach (var checkBox in checkBoxes)
  89. {
  90. checkBox.IsChecked = calcValue(checkBox);
  91. }
  92. }
  93. #endregion
  94. }
  95. public enum SelectType
  96. {
  97. UnDefine,
  98. All,
  99. None,
  100. Inverse
  101. }
  102. }