Clasher
Clasher

Reputation: 39

How to click a Button inside of a Button? C# WPF

I created some buttons with another button inside. Then I want to click the inner button but it only throws me the outer button click event, not even both. I tried the solutions in this question but didn't achieve anything. The exact goal that I have is to only load the click method of the inner button when i click it, and if I click in wherever else in the outer button it throws me the respective method.

        //Outer button click event

        void newBtn_Click(object sender, EventArgs e) {
        //Stuff
        }
        newBtn.Click += new RoutedEventHandler(newBtn_Click);
        //Inner button click event
        void editButton_Click(object sender, RoutedEventArgs e)
        {
            //Stuff
            editButton.Click += new RoutedEventHandler(editButton_Click);
            e.Handled = true;
        }
     //stuff
    

Upvotes: 0

Views: 575

Answers (1)

trix
trix

Reputation: 898

It would be better to share your code asking a question.

By the way this sample should work:

        <Button Background="Red" Width="100" Height="50" Click="Button_Click">
        
        <Button Background="Green" Margin="10" Width="50" Height="50" Click="Button_Click_1" />
        
    </Button>

In the event handler of the inner button simply add this, as suggested by the post you mentioned:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        e.Handled = true;
    }

This will stop the chain of Routed Events (Bubbling and Tunneling)

Upvotes: 1

Related Questions