Skip to content

Commit 31659be

Browse files
authored
fixes #3643 - added ClickEvent handler for the PopupBoxContent in SplitButton and mark event as handled (#3646)
1 parent e2b4caa commit 31659be

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

src/MaterialDesignThemes.Wpf/SplitButton.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static SplitButton()
1717

1818
public PopupBoxPlacementMode PopupPlacementMode
1919
{
20-
get => (PopupBoxPlacementMode) GetValue(PopupPlacementModeProperty);
20+
get => (PopupBoxPlacementMode)GetValue(PopupPlacementModeProperty);
2121
set => SetValue(PopupPlacementModeProperty, value);
2222
}
2323

@@ -116,7 +116,7 @@ public DataTemplateSelector SplitContentTemplateSelector
116116

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

@@ -136,6 +136,17 @@ public override void OnApplyTemplate()
136136
WeakEventManager<Button, RoutedEventArgs>.AddHandler(_rightButton, nameof(Click), OpenPopupBox);
137137
}
138138

139+
if (_popupBox is not null)
140+
{
141+
_popupBox.RemoveHandler(ButtonBase.ClickEvent, (RoutedEventHandler)PopupContentClickedHandler);
142+
_popupBox.AddHandler(ButtonBase.ClickEvent, (RoutedEventHandler)PopupContentClickedHandler);
143+
}
144+
145+
void PopupContentClickedHandler(object sender, RoutedEventArgs e)
146+
{
147+
e.Handled = true;
148+
}
149+
139150
void OpenPopupBox(object? sender, RoutedEventArgs e)
140151
{
141152
if (_popupBox is not null)

tests/MaterialDesignThemes.UITests/WPF/SplitButtons/SplitButtonTests.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using MaterialDesignThemes.UITests.Samples.SplitButton;
22

3-
[assembly:GenerateHelpers(typeof(SplitButtonWithCommandBinding))]
3+
[assembly: GenerateHelpers(typeof(SplitButtonWithCommandBinding))]
44

55
namespace MaterialDesignThemes.UITests.WPF.SplitButtons;
66

@@ -152,4 +152,41 @@ public async Task SplitButton_CommandCanExecuteFalse_DisablesButton()
152152

153153
recorder.Success();
154154
}
155+
156+
[Fact]
157+
public async Task SplitButton_ClickingPopupContent_DoesNotExecuteSplitButtonClick()
158+
{
159+
await using var recorder = new TestRecorder(App);
160+
161+
//Arrange
162+
IVisualElement<SplitButton> splitButton = await LoadXaml<SplitButton>("""
163+
<materialDesign:SplitButton VerticalAlignment="Bottom"
164+
Content="Split Button"
165+
Style="{StaticResource MaterialDesignRaisedLightSplitButton}">
166+
<materialDesign:SplitButton.PopupContent>
167+
<Button x:Name="PopupContent" />
168+
</materialDesign:SplitButton.PopupContent>
169+
</materialDesign:SplitButton>
170+
""");
171+
172+
IVisualElement<PopupBox> popupBox = await splitButton.GetElement<PopupBox>();
173+
IVisualElement<Button> popupContent = await splitButton.GetElement<Button>("PopupContent");
174+
175+
IEventRegistration splitButtonClickEvent = await splitButton.RegisterForEvent(ButtonBase.ClickEvent.Name);
176+
IEventRegistration popupContentClickEvent = await popupContent.RegisterForEvent(ButtonBase.ClickEvent.Name);
177+
178+
//Act
179+
await popupBox.LeftClick();
180+
//NB: give the popup some time to show
181+
await Wait.For(async () => await popupContent.GetIsVisible());
182+
await Wait.For(async () => await popupContent.GetActualHeight() > 10);
183+
await popupContent.LeftClick();
184+
await Task.Delay(50);
185+
186+
// Assert
187+
Assert.Empty(await splitButtonClickEvent.GetInvocations());
188+
Assert.Single(await popupContentClickEvent.GetInvocations());
189+
190+
recorder.Success();
191+
}
155192
}

0 commit comments

Comments
 (0)