|
1 |
| -using Microsoft.Xaml.Behaviors.Core; |
2 |
| - |
3 |
| -namespace MaterialDesignThemes.Wpf; |
| 1 | +namespace MaterialDesignThemes.Wpf; |
4 | 2 |
|
5 | 3 | [TemplatePart(Name = PopupBoxPartName, Type = typeof(PopupBox))]
|
| 4 | +[TemplatePart(Name = RightButtonPartName, Type = typeof(Button))] |
6 | 5 | public class SplitButton : Button
|
7 | 6 | {
|
8 | 7 | public const string PopupBoxPartName = "PART_PopupBox";
|
| 8 | + public const string RightButtonPartName = "PART_RightButton"; |
9 | 9 |
|
10 | 10 | static SplitButton()
|
11 | 11 | {
|
@@ -111,52 +111,38 @@ public DataTemplateSelector SplitContentTemplateSelector
|
111 | 111 | set => SetValue(SplitContentTemplateSelectorProperty, value);
|
112 | 112 | }
|
113 | 113 |
|
114 |
| - internal static readonly DependencyProperty ButtonStyleProperty = DependencyProperty.Register( |
| 114 | + public static readonly DependencyProperty ButtonStyleProperty = DependencyProperty.Register( |
115 | 115 | nameof(ButtonStyle), typeof(Style), typeof(SplitButton), new PropertyMetadata(default(Style)));
|
116 | 116 |
|
117 |
| - internal Style ButtonStyle |
| 117 | + public Style ButtonStyle |
118 | 118 | {
|
119 | 119 | get => (Style) GetValue(ButtonStyleProperty);
|
120 | 120 | set => SetValue(ButtonStyleProperty, value);
|
121 | 121 | }
|
122 | 122 |
|
123 |
| - internal static readonly DependencyProperty PopupBoxButtonClickedCommandProperty = DependencyProperty.Register( |
124 |
| - nameof(PopupBoxButtonClickedCommand), typeof(ICommand), typeof(SplitButton), new PropertyMetadata(default(ICommand))); |
125 |
| - |
126 |
| - internal ICommand PopupBoxButtonClickedCommand |
127 |
| - { |
128 |
| - get => (ICommand)GetValue(PopupBoxButtonClickedCommandProperty); |
129 |
| - set => SetValue(PopupBoxButtonClickedCommandProperty, value); |
130 |
| - } |
131 |
| - |
132 | 123 | private PopupBox? _popupBox;
|
133 |
| - |
134 |
| - public SplitButton() |
135 |
| - { |
136 |
| - PopupBoxButtonClickedCommand = new ActionCommand(OpenPopupBox); |
137 |
| - } |
| 124 | + private Button? _rightButton; |
138 | 125 |
|
139 | 126 | public override void OnApplyTemplate()
|
140 | 127 | {
|
141 | 128 | base.OnApplyTemplate();
|
142 | 129 |
|
143 | 130 | _popupBox = GetTemplateChild(PopupBoxPartName) as PopupBox;
|
| 131 | + _rightButton = GetTemplateChild(RightButtonPartName) as Button; |
144 | 132 |
|
145 |
| - if (_popupBox is not null) |
| 133 | + if (_rightButton is not null) |
146 | 134 | {
|
147 |
| - _popupBox.RemoveHandler(ButtonBase.ClickEvent, (RoutedEventHandler)ChildClickedHandler); |
148 |
| - _popupBox.AddHandler(ButtonBase.ClickEvent, (RoutedEventHandler)ChildClickedHandler); |
| 135 | + WeakEventManager<Button, RoutedEventArgs>.RemoveHandler(_rightButton, nameof(Click), OpenPopupBox); |
| 136 | + WeakEventManager<Button, RoutedEventArgs>.AddHandler(_rightButton, nameof(Click), OpenPopupBox); |
149 | 137 | }
|
150 |
| - } |
151 | 138 |
|
152 |
| - private static void ChildClickedHandler(object sender, RoutedEventArgs e) |
153 |
| - => e.Handled = true; |
154 |
| - |
155 |
| - private void OpenPopupBox() |
156 |
| - { |
157 |
| - if (_popupBox is not null) |
| 139 | + void OpenPopupBox(object? sender, RoutedEventArgs e) |
158 | 140 | {
|
159 |
| - _popupBox.IsPopupOpen = true; |
| 141 | + if (_popupBox is not null) |
| 142 | + { |
| 143 | + _popupBox.IsPopupOpen = true; |
| 144 | + e.Handled = true; |
| 145 | + } |
160 | 146 | }
|
161 | 147 | }
|
162 | 148 | }
|
0 commit comments