Listen to child MenuItem Click

爷,独闯天下 提交于 2019-12-01 12:09:42

MenuItem.Click is a routed event (it bubbles up), so you can just subscribe to it on the first MenuItem and be notified of all children at the same time.

XAML:

<MenuItem Name='a' Click='OnMenuItemClicked'>
    <MenuItem Name='b' Header='...' />
</MenuItem>

C#:

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

The trick is to use RoutedEventArgs.OriginalSource, rather than sender. This points to the control that originally fired the event.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!