【问题标题】:catch button event from usercontrol从用户控件捕获按钮事件
【发布时间】:2013-05-21 09:59:57
【问题描述】:

我有一个名为 mybutton 的按钮,它是用户控件 X 的一部分。 我制作了另一个包含 X 的用户控件 Y(X 是其中的一部分)。

我想在点击时捕捉到 mybutton 的事件。

我该怎么做?

谢谢

【问题讨论】:

    标签: wpf button user-controls click


    【解决方案1】:

    看下面的代码。 UserControlGrid 相同。 Click 事件沿可视树向上路由,阅读更多 here

    <Grid Button.Click="Button_Clicked">
        <Grid>
            <Button Content="Save" />
        </Grid>
    </Grid>
    
    
    private void Button_Clicked(object sender, RoutedEventArgs e)
    {
        //your code here
    }
    

    【讨论】:

    • 我会解释自己:我在一个 xaml 中有: 现在我在另一个 xaml 用户控件>。如何在 usercontrol Y 后面的代码中获取 x_click 事件?非常感谢
    • 没区别,在你的 Y 中附加一个Button.Click="Button_Clicked" UserControl 像这样:&lt;Usercontrol Name=Y&gt;&lt;X Button.Click="Button_Clicked"&gt;&lt;/X&gt;&lt;/UserControl&gt;
    • 非常感谢。在职的。仅据我所知:如果我在 X 中有两个按钮怎么办?我应该根据按钮名称放入“Button_Clicked”一个开关盒吗?还是一种优雅的方式?
    • 如果有多个Buttons,最好使用RoutedCommands。在这种情况下,您可以给每个Button 一个不同的Command 并为每个Command 设置一个处理程序。原理是一样的——Command 向上路由可视化树。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多