如何在usercontrol中创建一个事件并将其置于主窗体中 [英] How do I make an event in the usercontrol and have it handeled in the main form
问题描述
我在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();
}
推荐答案
只需添加一个事件,然后获取容器表单来处理它:添加事件的简单代码段 [ ^ ] - 它显示了您需要的代码,以及如何在VS中自动化。
Just add an event and then get the container form to handle it: A Simple Code Snippet to Add an Event[^] - it shows the code you need, and how to "automate it" in VS.
这篇关于如何在usercontrol中创建一个事件并将其置于主窗体中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!