Boris
Boris

Reputation: 10234

Stop event bubbling up using XAML

I have a Grid with a Button inside. The button has Flyout menu attached.

I implemented an action which opens the flyout menu when the button is tapped/clicked. This is the default behavior which does not require event writing. I also implemented an action when the grid is tapped/clicked.

The problem is that I do not want the grid to react when I tap/click the button. Based on this fine read, it all makes sense, but in my case, I do not have any code behind to add the e.Handled = true; line to.

Is there any way I could stop event bubbling up the tree using XAML only? Thanks!

Upvotes: 2

Views: 1551

Answers (2)

Chris W.
Chris W.

Reputation: 23270

While I hate to poach Gusdor's points. There is a built an enumeration property to deal with this types of situations called ClickMode which you can override the default mode for Button of Release and set it at the instance as ClickMode="Press" to get the desired effect and allow it to receive HitTestVisibility individually before any parent does.

Hope this helps, cheers.

Upvotes: 2

Gusdor
Gusdor

Reputation: 14334

I believe you will need to write some code but not the code behind you are trying to avoid.

  1. Create an attached behavior that subscribes to, and handles the bubbling event.
  2. Attach the behaviour to the element where you want the event bubbling to stop.

There is a Microsoft article about plugging Behaviours into UWP apps https://blogs.windows.com/buildingapps/2015/11/30/xaml-behaviors-open-source-and-on-uwp/

Upvotes: 0

Related Questions