简体   繁体   English

如何单击按钮内的按钮? C# WPF

[英]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.我的确切目标是仅在我单击内部按钮时加载它的 click 方法,如果我单击外部按钮中的其他任何位置,它会向我抛出相应的方法。

        //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
    

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)这将停止路由事件链(冒泡和隧道)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM