Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Button inside a button Click issue

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.

like image 806
Sepak Avatar asked Sep 26 '22 02:09

Sepak


1 Answers

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;
}
like image 116
AnjumSKhan Avatar answered Sep 29 '22 01:09

AnjumSKhan