|
| 1 | +using System.Windows.Input; |
| 2 | + |
| 3 | +namespace DevWinUI; |
| 4 | + |
| 5 | +public partial class RichButton : Control |
| 6 | +{ |
| 7 | + private const string PART_Button = "PART_Button"; |
| 8 | + private const string PART_TitleStackPanel = "PART_TitleStackPanel"; |
| 9 | + |
| 10 | + private Button button; |
| 11 | + private StackPanel stackPanel; |
| 12 | + |
| 13 | + public event EventHandler<RoutedEventArgs> Click; |
| 14 | + |
| 15 | + public ICommand Command |
| 16 | + { |
| 17 | + get { return (ICommand)GetValue(CommandProperty); } |
| 18 | + set { SetValue(CommandProperty, value); } |
| 19 | + } |
| 20 | + |
| 21 | + public static readonly DependencyProperty CommandProperty = |
| 22 | + DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(RichButton), new PropertyMetadata(null)); |
| 23 | + |
| 24 | + public object CommandParameter |
| 25 | + { |
| 26 | + get { return (object)GetValue(CommandParameterProperty); } |
| 27 | + set { SetValue(CommandParameterProperty, value); } |
| 28 | + } |
| 29 | + |
| 30 | + public static readonly DependencyProperty CommandParameterProperty = |
| 31 | + DependencyProperty.Register(nameof(CommandParameter), typeof(object), typeof(RichButton), new PropertyMetadata(null)); |
| 32 | + |
| 33 | + public object ActionIcon |
| 34 | + { |
| 35 | + get { return (object)GetValue(ActionIconProperty); } |
| 36 | + set { SetValue(ActionIconProperty, value); } |
| 37 | + } |
| 38 | + |
| 39 | + public static readonly DependencyProperty ActionIconProperty = |
| 40 | + DependencyProperty.Register(nameof(ActionIcon), typeof(object), typeof(RichButton), new PropertyMetadata(null)); |
| 41 | + |
| 42 | + public object Icon |
| 43 | + { |
| 44 | + get { return (object)GetValue(IconProperty); } |
| 45 | + set { SetValue(IconProperty, value); } |
| 46 | + } |
| 47 | + |
| 48 | + public static readonly DependencyProperty IconProperty = |
| 49 | + DependencyProperty.Register(nameof(Icon), typeof(object), typeof(RichButton), new PropertyMetadata(null, OnPropertyChanged)); |
| 50 | + |
| 51 | + private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 52 | + { |
| 53 | + var ctl = (RichButton)d; |
| 54 | + if (ctl != null) |
| 55 | + { |
| 56 | + ctl.UpdateMargin(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public object Title |
| 61 | + { |
| 62 | + get { return (object)GetValue(TitleProperty); } |
| 63 | + set { SetValue(TitleProperty, value); } |
| 64 | + } |
| 65 | + |
| 66 | + public static readonly DependencyProperty TitleProperty = |
| 67 | + DependencyProperty.Register(nameof(Title), typeof(object), typeof(RichButton), new PropertyMetadata(null, OnPropertyChanged)); |
| 68 | + |
| 69 | + public object SubTitle |
| 70 | + { |
| 71 | + get { return (object)GetValue(SubTitleProperty); } |
| 72 | + set { SetValue(SubTitleProperty, value); } |
| 73 | + } |
| 74 | + |
| 75 | + public static readonly DependencyProperty SubTitleProperty = |
| 76 | + DependencyProperty.Register(nameof(SubTitle), typeof(object), typeof(RichButton), new PropertyMetadata(null, OnPropertyChanged)); |
| 77 | + |
| 78 | + public Brush TitleForeground |
| 79 | + { |
| 80 | + get { return (Brush)GetValue(TitleForegroundProperty); } |
| 81 | + set { SetValue(TitleForegroundProperty, value); } |
| 82 | + } |
| 83 | + |
| 84 | + public static readonly DependencyProperty TitleForegroundProperty = |
| 85 | + DependencyProperty.Register(nameof(TitleForeground), typeof(Brush), typeof(RichButton), new PropertyMetadata(null)); |
| 86 | + |
| 87 | + public Brush SubTitleForeground |
| 88 | + { |
| 89 | + get { return (Brush)GetValue(SubTitleForegroundProperty); } |
| 90 | + set { SetValue(SubTitleForegroundProperty, value); } |
| 91 | + } |
| 92 | + |
| 93 | + public static readonly DependencyProperty SubTitleForegroundProperty = |
| 94 | + DependencyProperty.Register(nameof(SubTitleForeground), typeof(Brush), typeof(RichButton), new PropertyMetadata(null)); |
| 95 | + |
| 96 | + public RichButtonDisplayMode DisplayMode |
| 97 | + { |
| 98 | + get { return (RichButtonDisplayMode)GetValue(DisplayModeProperty); } |
| 99 | + set { SetValue(DisplayModeProperty, value); } |
| 100 | + } |
| 101 | + |
| 102 | + public static readonly DependencyProperty DisplayModeProperty = |
| 103 | + DependencyProperty.Register(nameof(DisplayMode), typeof(RichButtonDisplayMode), typeof(RichButton), new PropertyMetadata(RichButtonDisplayMode.Subtle, OnDisplayModeChanged)); |
| 104 | + |
| 105 | + private static void OnDisplayModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 106 | + { |
| 107 | + var ctl = (RichButton)d; |
| 108 | + if (ctl != null) |
| 109 | + { |
| 110 | + ctl.UpdateDisplayMode(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + protected override void OnApplyTemplate() |
| 116 | + { |
| 117 | + base.OnApplyTemplate(); |
| 118 | + |
| 119 | + button = GetTemplateChild(PART_Button) as Button; |
| 120 | + stackPanel = GetTemplateChild(PART_TitleStackPanel) as StackPanel; |
| 121 | + |
| 122 | + button.Click -= OnClick; |
| 123 | + button.Click += OnClick; |
| 124 | + |
| 125 | + UpdateDisplayMode(); |
| 126 | + |
| 127 | + UpdateMargin(); |
| 128 | + } |
| 129 | + private void UpdateMargin() |
| 130 | + { |
| 131 | + if (stackPanel == null) |
| 132 | + return; |
| 133 | + |
| 134 | + if (Icon != null && (Title != null || SubTitle != null)) |
| 135 | + stackPanel.Margin = new(16, 0, 0, 0); |
| 136 | + else if (Icon != null) |
| 137 | + stackPanel.Margin = new(0, 0, 0, 0); |
| 138 | + else |
| 139 | + stackPanel.Margin = new(0, 0, 0, 0); |
| 140 | + } |
| 141 | + |
| 142 | + private void OnClick(object sender, RoutedEventArgs e) |
| 143 | + { |
| 144 | + Click?.Invoke(this, e); |
| 145 | + } |
| 146 | + |
| 147 | + private void UpdateDisplayMode() |
| 148 | + { |
| 149 | + if (button == null) |
| 150 | + return; |
| 151 | + |
| 152 | + switch (DisplayMode) |
| 153 | + { |
| 154 | + case RichButtonDisplayMode.Normal: |
| 155 | + button.Style = Application.Current.Resources["DefaultButtonStyle"] as Style; |
| 156 | + |
| 157 | + break; |
| 158 | + case RichButtonDisplayMode.Subtle: |
| 159 | + button.Style = Application.Current.Resources["SubtleButtonStyle"] as Style; |
| 160 | + |
| 161 | + break; |
| 162 | + case RichButtonDisplayMode.ReadOnly: |
| 163 | + button.Style = Application.Current.Resources["NoEffectSubtleButtonStyle"] as Style; |
| 164 | + |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments