48 c# silverlight events user-controls raiseevent
我有一个UserControl
,我需要通知父页面点击了一个按钮UserControl
.如何UserControl
在主页面上引发事件并将其捕获?我尝试过使用static
,很多人建议我去参加活动!
Jem*_*mes 86
查看Event Bubbling - http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx
编辑以添加一个快速示例:(和其他编辑以改进格式)
用户控制
public event EventHandler StatusUpdated;
private void FunctionThatRaisesEvent()
{
//Null check makes sure the main page is attached to the event
if (this.StatusUpdated != null)
this.StatusUpdated(this, new EventArgs());
}
Run Code Online (Sandbox Code Playgroud)
主页/表格
public void MyApp()
{
//USERCONTROL = your control with the StatusUpdated event
this.USERCONTROL.StatusUpdated += new EventHandler(MyEventHandlerFunction_StatusUpdated);
}
public void MyEventHandlerFunction_StatusUpdated(object sender, EventArgs e)
{
//your code here
}
Run Code Online (Sandbox Code Playgroud)
只需在您的控件中添加一个事件:
public event EventHandler SomethingHappened;
Run Code Online (Sandbox Code Playgroud)
当你想通知父母时提出它:
if(SomethingHappened != null) SomethingHappened(this, new EventArgs);
Run Code Online (Sandbox Code Playgroud)
如果您需要自定义EventArgs,请尝试EventHandler<T>
使用T
派生自的类型EventArgs
.