Skip to content

Commit

Permalink
fixes MaterialDesignInXAML#3643 - added ClickEvent handler for the Po…
Browse files Browse the repository at this point in the history
…pupBoxContent in SplitButton and mark event as handled (MaterialDesignInXAML#3646)
  • Loading branch information
corvinsz authored Aug 8, 2024
1 parent e2b4caa commit 31659be
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/MaterialDesignThemes.Wpf/SplitButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static SplitButton()

public PopupBoxPlacementMode PopupPlacementMode
{
get => (PopupBoxPlacementMode) GetValue(PopupPlacementModeProperty);
get => (PopupBoxPlacementMode)GetValue(PopupPlacementModeProperty);
set => SetValue(PopupPlacementModeProperty, value);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public DataTemplateSelector SplitContentTemplateSelector

public Style ButtonStyle
{
get => (Style) GetValue(ButtonStyleProperty);
get => (Style)GetValue(ButtonStyleProperty);
set => SetValue(ButtonStyleProperty, value);
}

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

if (_popupBox is not null)
{
_popupBox.RemoveHandler(ButtonBase.ClickEvent, (RoutedEventHandler)PopupContentClickedHandler);
_popupBox.AddHandler(ButtonBase.ClickEvent, (RoutedEventHandler)PopupContentClickedHandler);
}

void PopupContentClickedHandler(object sender, RoutedEventArgs e)
{
e.Handled = true;
}

void OpenPopupBox(object? sender, RoutedEventArgs e)
{
if (_popupBox is not null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using MaterialDesignThemes.UITests.Samples.SplitButton;

[assembly:GenerateHelpers(typeof(SplitButtonWithCommandBinding))]
[assembly: GenerateHelpers(typeof(SplitButtonWithCommandBinding))]

namespace MaterialDesignThemes.UITests.WPF.SplitButtons;

Expand Down Expand Up @@ -152,4 +152,41 @@ public async Task SplitButton_CommandCanExecuteFalse_DisablesButton()

recorder.Success();
}

[Fact]
public async Task SplitButton_ClickingPopupContent_DoesNotExecuteSplitButtonClick()
{
await using var recorder = new TestRecorder(App);

//Arrange
IVisualElement<SplitButton> splitButton = await LoadXaml<SplitButton>("""
<materialDesign:SplitButton VerticalAlignment="Bottom"
Content="Split Button"
Style="{StaticResource MaterialDesignRaisedLightSplitButton}">
<materialDesign:SplitButton.PopupContent>
<Button x:Name="PopupContent" />
</materialDesign:SplitButton.PopupContent>
</materialDesign:SplitButton>
""");

IVisualElement<PopupBox> popupBox = await splitButton.GetElement<PopupBox>();
IVisualElement<Button> popupContent = await splitButton.GetElement<Button>("PopupContent");

IEventRegistration splitButtonClickEvent = await splitButton.RegisterForEvent(ButtonBase.ClickEvent.Name);
IEventRegistration popupContentClickEvent = await popupContent.RegisterForEvent(ButtonBase.ClickEvent.Name);

//Act
await popupBox.LeftClick();
//NB: give the popup some time to show
await Wait.For(async () => await popupContent.GetIsVisible());
await Wait.For(async () => await popupContent.GetActualHeight() > 10);
await popupContent.LeftClick();
await Task.Delay(50);

// Assert
Assert.Empty(await splitButtonClickEvent.GetInvocations());
Assert.Single(await popupContentClickEvent.GetInvocations());

recorder.Success();
}
}

0 comments on commit 31659be

Please sign in to comment.