在.NET C#中从用户控件引发父窗口上的事件

use*_*392 16 .net wpf events user-controls

标题几乎解释了这个问题.首次运行应用程序时,我将用户控件加载到主窗口中.我想要做的是在单击用户控件上的按钮时在父窗口上引发事件,那么如何从用户控件上的button1_Click引发父事件?

Joh*_*ten 48

这个问题似乎没有在任何地方端到端地完整描述:上面提供的链接 并不是如何定义,引发和捕获从UserControl到父窗口的事件的完整说明.对某些人来说可能是显而易见的,但是我花了一分钟来连接点,所以我将分享我的发现.是的,这样做的方法是通过自定义RoutedEvent设置为RoutingStrategy.Bubble,但还有更多内容.


在这个例子中,我有一个名为ServerConfigs的UserControl,它有一个Save按钮.当用户单击"保存"按钮时,我想要一个父窗口上的按钮,其中包含用于启用另一个按钮的UserControl,该按钮称为btn_synchronize.

在后面的UserControl代码上定义以下内容,按照上面引用的上述RoutedEvent链接.

public partial class ServerConfigs : UserControl
{

    // Create RoutedEvent
    // This creates a static property on the UserControl, SettingsConfirmedEvent, which 
    // will be used by the Window, or any control up the Visual Tree, that wants to 
    // handle the event. This is the Custom Routed Event for more info on the 
    // RegisterRoutedEvent method
    // https://msdn.microsoft.com/en-us/library/ms597876(v=vs.100).aspx

    public static readonly RoutedEvent SettingConfirmedEvent =
        EventManager.RegisterRoutedEvent("SettingConfirmedEvent", RoutingStrategy.Bubble,
        typeof(RoutedEventHandler), typeof(ServerConfigs));

    // Create RoutedEventHandler
    // This adds the Custom Routed Event to the WPF Event System and allows it to be 
    // accessed as a property from within xaml if you so desire

    public event RoutedEventHandler SettingConfirmed
    {
        add { AddHandler(SettingConfirmedEvent, value); }
        remove { RemoveHandler(SettingConfirmedEvent, value); }
    }
    ....

    // When the Save button on the User Control is clicked, use RaiseEvent to fire the 
    // Custom Routed Event

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
    ....
        // Raise the custom routed event, this fires the event from the UserControl
        RaiseEvent(new RoutedEventArgs(ServerConfigs.SettingConfirmedEvent));
    ....
    }
}
Run Code Online (Sandbox Code Playgroud)

有一个实现示例,上面的教程结束了.那么如何在窗口上捕获事件并处理它呢?

从window.xaml开始,这就是如何使用用户控件中定义的Custom RoutedEventHandler.这就像button.click等.

<Window>
....
<uc1:ServerConfigs SettingConfirmed="Window_UserControl_SettingConfirmedEventHandlerMethod" 
                   x:Name="ucServerConfigs" ></uc1:ServerConfigs>
...
</Window>
Run Code Online (Sandbox Code Playgroud)

或者在Window代码后面,您可以通过AddHandler设置EventHandler,通常在构造函数中:

public Window()
{
    InitializeComponent();
    // Register the Bubble Event Handler 
    AddHandler(ServerConfigs.SettingConfirmedEvent, 
        new RoutedEventHandler(Window_UserControl_SettingConfirmedEventHandlerMethod));
}

private void Window_UserControl_SettingConfirmedEventHandlerMethod(object sender,
              RoutedEventArgs e)
{
    btn_synchronize.IsEnabled = true;
}
Run Code Online (Sandbox Code Playgroud)

  • 最终使RoutedEvents能够“单击”-这个答案涵盖了从头到尾的整个过程。好东西。 (2认同)

Dan*_*iel 5

您需要一个RoutedEvent 链接

“路由事件是根据其路由策略在视觉树中向上或向下导航的事件。路由策略可以是气泡,隧道或直接。您可以将事件处理程序挂接到引发事件的元素上,也可以将其绑定到上方或下方的其他元素上通过使用附加的事件语法:Button.Click =“ Button_Click”。“