如何在usercontrol中创建一个事件

如何在usercontrol中创建一个事件

本文介绍了如何在usercontrol中创建一个事件并将其置于主窗体中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf中有一个应用程序,我使用UserControl来填充一些数据。我需要在父窗口中执行一些操作,同时单击UserControl中的按钮。

我尝试使用事件和代理但是无法在父窗口中调用该功能。



我尝试过:



在我的usercontrol中我有这样的代码

I have an application in wpf where I use UserControl to fill some data.I need some actions performed in the parent window while clicking on a button in UserControl.
I tried using events and delegates but failed to call the function in parent window.

What I have tried:

in my usercontrol I have code like this

// A delegate type for hooking up change notifications.
    public delegate void Button_ClickedEventHandler(object sender, EventArgs e);



..........

。 ........


..........
.........

// An event that clients can use to be notified whenever the
       // the button is clicked
       public event Button_ClickedEventHandler ButtonClicked;



................

....... .........




................
................

// Invoke the event; called whenever button clicked
public void button1_Click(object sender, RoutedEventArgs e)
       {

              if (this.ButtonClicked != null)
               this.ButtonClicked(this,e);
       }



.......................... ................

和父窗口


..........................................
and in the parent window

public myWindow()
       {
           InitializeComponent();
           myClass1.ButtonClicked += new Button_ClickedEventHandler(myClass1_ButtonClicked);

       }




void myClass1_ButtonClicked(object sender, EventArgs e)
       {
           throw new NotImplementedException();
       }

推荐答案


这篇关于如何在usercontrol中创建一个事件并将其置于主窗体中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:15