using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace FWindSoft.Wpf.Controls { public class ButtonOptions { public static readonly DependencyProperty IconProperty = DependencyProperty.RegisterAttached("Icon", typeof(ImageSource), typeof(ButtonOptions), new PropertyMetadata(null, OnShowChanged)); [AttachedPropertyBrowsableForType(typeof(Button))] public static ImageSource GetIcon(DependencyObject dp) { return dp.GetValue(IconProperty) as ImageSource; } [AttachedPropertyBrowsableForType(typeof(Button))] public static void SetIcon(DependencyObject dp, object value) { dp.SetValue(IconProperty,value); } static ButtonOptions() { } public static void OnShowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { TextBox textBox = d as TextBox; if (textBox == null) return; } #region 子对象CheckBox控制 public static readonly DependencyProperty SelectTypeProperty = DependencyProperty.RegisterAttached("SelectType", typeof(SelectType), typeof(ButtonOptions), new PropertyMetadata(SelectType.UnDefine, OnSelectTypeChanged)); [AttachedPropertyBrowsableForType(typeof(Button))] public static SelectType GetSelectType(DependencyObject dp) { return (SelectType)dp.GetValue(SelectTypeProperty); } [AttachedPropertyBrowsableForType(typeof(Button))] public static void SetSelectType(DependencyObject dp, SelectType value) { dp.SetValue(SelectTypeProperty, value); } public static readonly DependencyProperty CheckBoxContainerProperty = DependencyProperty.RegisterAttached("CheckBoxContainer", typeof(UIElement), typeof(ButtonOptions), new PropertyMetadata(null)); [AttachedPropertyBrowsableForType(typeof(Button))] public static UIElement GetCheckBoxContainer(DependencyObject dp) { return (UIElement)dp.GetValue(CheckBoxContainerProperty); } [AttachedPropertyBrowsableForType(typeof(Button))] public static void SetCheckBoxContainer(DependencyObject dp, UIElement value) { dp.SetValue(CheckBoxContainerProperty, value); } public static void OnSelectTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Button button = d as Button; if (button == null) return; var selectType = (SelectType)e.NewValue; button.Click -= Button_Click; if(selectType!= SelectType.UnDefine) { button.Click += Button_Click; } } private static void Button_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; if (button == null) return; var container = GetCheckBoxContainer(button); if (container == null) return; var checkBoxes = container.GetSpecifyTypeChildren(); var selectType = GetSelectType(button); if (selectType == SelectType.UnDefine) return; Func calcValue = (c) => false; if (selectType == SelectType.All) { calcValue=(c) => true; } else if (selectType == SelectType.Inverse) { calcValue = (c) => !(c.IsChecked == true); } foreach (var checkBox in checkBoxes) { checkBox.IsChecked = calcValue(checkBox); } } #endregion } public enum SelectType { UnDefine, All, None, Inverse } }