Skip to content

Commit 845ba63

Browse files
Minor refactor of SplitButton (#3415)
* Minor refactor of SplitButton Removed use of MVVM in internal Click handling and made ButtonStyle public to allow consumers to copy-paste-modify the style if they so desire. * Removed wrongfully committed method
1 parent f50dfbd commit 845ba63

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

MaterialDesignThemes.Wpf/SplitButton.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Microsoft.Xaml.Behaviors.Core;
2-
3-
namespace MaterialDesignThemes.Wpf;
1+
namespace MaterialDesignThemes.Wpf;
42

53
[TemplatePart(Name = PopupBoxPartName, Type = typeof(PopupBox))]
4+
[TemplatePart(Name = RightButtonPartName, Type = typeof(Button))]
65
public class SplitButton : Button
76
{
87
public const string PopupBoxPartName = "PART_PopupBox";
8+
public const string RightButtonPartName = "PART_RightButton";
99

1010
static SplitButton()
1111
{
@@ -111,52 +111,38 @@ public DataTemplateSelector SplitContentTemplateSelector
111111
set => SetValue(SplitContentTemplateSelectorProperty, value);
112112
}
113113

114-
internal static readonly DependencyProperty ButtonStyleProperty = DependencyProperty.Register(
114+
public static readonly DependencyProperty ButtonStyleProperty = DependencyProperty.Register(
115115
nameof(ButtonStyle), typeof(Style), typeof(SplitButton), new PropertyMetadata(default(Style)));
116116

117-
internal Style ButtonStyle
117+
public Style ButtonStyle
118118
{
119119
get => (Style) GetValue(ButtonStyleProperty);
120120
set => SetValue(ButtonStyleProperty, value);
121121
}
122122

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-
132123
private PopupBox? _popupBox;
133-
134-
public SplitButton()
135-
{
136-
PopupBoxButtonClickedCommand = new ActionCommand(OpenPopupBox);
137-
}
124+
private Button? _rightButton;
138125

139126
public override void OnApplyTemplate()
140127
{
141128
base.OnApplyTemplate();
142129

143130
_popupBox = GetTemplateChild(PopupBoxPartName) as PopupBox;
131+
_rightButton = GetTemplateChild(RightButtonPartName) as Button;
144132

145-
if (_popupBox is not null)
133+
if (_rightButton is not null)
146134
{
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);
149137
}
150-
}
151138

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)
158140
{
159-
_popupBox.IsPopupOpen = true;
141+
if (_popupBox is not null)
142+
{
143+
_popupBox.IsPopupOpen = true;
144+
e.Handled = true;
145+
}
160146
}
161147
}
162148
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.SplitButton.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
PlacementTarget="{Binding ElementName=OuterGrid}"
7272
Opacity="1">
7373
<wpf:PopupBox.ToggleContent>
74-
<Button Command="{TemplateBinding PopupBoxButtonClickedCommand}"
74+
<Button x:Name="PART_RightButton"
7575
BorderThickness="{TemplateBinding BorderThickness}"
7676
Height="{Binding ElementName=PART_LeftButton, Path=ActualHeight}"
7777
IsHitTestVisible="True"

0 commit comments

Comments
 (0)