ali khezri
ali khezri

Reputation: 453

How can I set click event for a child MenuItem without raise parent event?

I have this code :

<Menu IsMainMenu="True">
    <MenuItem Header="Parent" Click="ParentItem_Click" >
        <MenuItem Header="Child" Click="ChildItem_Click">
        </MenuItem>
    </MenuItem>
</Menu>

When I click child MenuItem, the parent MenuItem will be raise too. I don't want that parent event raised.

Upvotes: 2

Views: 2168

Answers (3)

user2725796
user2725796

Reputation: 11

I know this is 4 years and a half old, but the solution was pretty easy (I just figured it out). Just give names to the items.

XAML

<Menu IsMainMenu="True">
    <MenuItem x:Name="Parent" Header="Parent" Click="ParentItem_Click" >
        <MenuItem x:Name="Child" Header="Child" Click="ChildItem_Click">
        </MenuItem>
    </MenuItem>
</Menu>

CODE

private void ParentItem_Click(object sender, RoutedEventArgs e)
{
     MenuItem item = e.OriginalSource as MenuItem;
     if(item == Parent)
     {
         // Handle Parent
     }
}

private void ChildItem_Click(object sender, RoutedEventArgs e)
{
     MenuItem item = e.OriginalSource as MenuItem;
     if(item == Child)
     {
         // Handle Child
     }
}

Hope this helps someone!

Upvotes: 0

sll
sll

Reputation: 62544

This is standard behaviour for events with Bubbling routing strategy. You can manually set eventArgs.Handled = true in the event handler. See Marking Routed Events as Handled, and Class Handling

Considering that Click event is not paired with a tunneling event like PreviewClick - you can try out using PreviewMouseDown PreviewMouseUp tunneling events and see whether you can attach your logic to these events.

Routed event strategies:

Bubbling: Event handlers on the event source are invoked. The routed event then routes to successive parent elements until reaching the element tree root. Most routed events use the bubbling routing strategy. Bubbling routed events are generally used to report input or state changes from distinct controls or other UI elements.

Direct: Only the source element itself is given the opportunity to invoke handlers in response. This is analogous to the "routing" that Windows Forms uses for events. However, unlike a standard CLR event, direct routed events support class handling (class handling is explained in an upcoming section) and can be used by EventSetter and EventTrigger.

Tunneling: Initially, event handlers at the element tree root are invoked. The routed event then travels a route through successive child elements along the route, towards the node element that is the routed event source (the element that raised the routed event). Tunneling routed events are often used or handled as part of the compositing for a control, such that events from composite parts can be deliberately suppressed or replaced by events that are specific to the complete control. Input events provided in WPF often come implemented as a tunneling/bubbling pair. Tunneling events are also sometimes referred to as Preview events, because of a naming convention that is used for the pairs.

Upvotes: 5

Lonli-Lokli
Lonli-Lokli

Reputation: 3784

As click is routed, you may use this code

private void OnYourMenuItemClicked(object sender, RoutedEventArgs e){
     MenuItem item = e.OriginalSource as MenuItem;
     if(null != item)
     {
         // Handle needed menu item click here
     }

}

Upvotes: 3

Related Questions