Peri Hartman
Peri Hartman

Reputation: 19474

uwp: how to pass Button click event to default handler

I would like to handle the Button Click event, do something, and then let the default handler execute.

Here's what I have:

<Button ... Click="OnMoreClicked" />

And here's my handler

void onMoreClicked (object sender, RoutedEventArgs e)
{
  ... do something ...
  ... continue with default button handler ...
}

EDIT:

The button activates a flyout, thus it already has a handler to do that and I want to ensure that handler gets executed.

EDIT 2: my example code was too minimal. See this, instead:

<Button ... Click="onMoreClicked">
  <Button.Flyout > 
   ...
  </Button.Flyout>
</Button>

And

public void onMoreClicked (object sender, RoutedEventArgs e)
{
  ... do something ...
  ... call default handler to show flyout ...
}

Upvotes: 0

Views: 397

Answers (1)

mm8
mm8

Reputation: 169160

The Click event handler is called before the Flyout is shown.

So you for example change the Background of the Button in the event handler:

private void onMoreClicked(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    button.Background = new SolidColorBrush(Colors.AliceBlue);
}

...and still see the Flyout:

enter image description here

You may also handle the Opened, Opening, Closed or Closing events of the MenuFlyout.

Or use an AttachedFlyout and show it programmatically using the ShowAttachedFlyout method:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    //...
    Flyout.ShowAttachedFlyout(button);
}

XAML:

<Button Content="Button With Flyout" Click="Button_Click">
    <FlyoutBase.AttachedFlyout>
        <MenuFlyout>
            <MenuFlyoutItem Text="Edit" />
        </MenuFlyout>
    </FlyoutBase.AttachedFlyout>
</Button>

Upvotes: 1

Related Questions