WPF Button inside a button Click issue

拈花ヽ惹草 提交于 2019-12-12 18:43:16

问题


I have the following structure (very simplified) in my WPF project:

Button newProduct = new Button();
Grid newGrid = new Grid();
Button modify = new Button();
Button remove = new Button();
newGrid.Children.add(modify);
newGrid.Children.add(remove);
newProduct.Content = newGrid;   

Each button has its own click event. There are textblocks inside the button as well. NewProduct highlights its content, modify changes the textblocks info and remove deletes the main button from the view.

My problem is that whenever I click one of the buttons inside the bigger one(modify, remove), the click event from the bigger one, newProduct, also fires. The newProduct click event changes its size according to what is needed and I don't need it to do anything when modify or remove is clicked.

My question is. How can I make that if one of the buttons inside of the main button is clicked, only their respective click event is fired, and not the one from the main one?

Is there a way to tell newProduct to ignore its click event when the mouse is over one of its childs?

Thank you and I offer my apologies if this has been answered, I searched a lot and didn't find what I needed.


回答1:


Mark inner button's click event as handled like e.Handled = true; at the end of your handlers.


Your inner Button's Click handler :

private void Button_Click(object sender, RoutedEventArgs e)
{
    // some logic
    e.Handled = true;
}


来源:https://stackoverflow.com/questions/33967576/wpf-button-inside-a-button-click-issue

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