I have a UserControl
, and I need to notify the parent page that a button in the UserControl
was clicked. How do I raise an event in the UserControl
and catch it on the Main page? I tried using static
, and many suggested me to go for events!
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Just add an event in your control:
and raise it when you want to notify the parent:
If you need custom EventArgs try
EventHandler<T>
instead withT
beeing a type derived fromEventArgs
.Or if you are looking for a more decoupled solution you can use a messenger publisher / subscriber model such as MVVM Light Messenger here
Check out Event Bubbling -- http://msdn.microsoft.com/en-us/library/aa719644%28vs.71%29.aspx
Edit to add a quick example: (and additional edit to improve formatting)
User Control
Main Page/Form