i\'ve created an UserControl for Database connection where user input Username and Password for a connection. This UserControl is in a MainWindow.xaml
Now, in code behin
First you should define a delegate and then use that delegate to define that event.
In your MyUserControl.xaml.cs file add the following
Option 1
public delegate void MyPersonalizedUCEventHandler(string sampleParam);
public event MyPersonalizedUCEventHandler MyPersonalizedUCEvent;
public void RaiseMyEvent()
{
// Your logic
if (MyPersonalizedUCEvent != null)
{
MyPersonalizedUCEvent("sample parameter");
}
}
And that's it. You have defined your event.
Option 2
public event Action MyPersonalizedUCEvent;
public void RaiseMyEvent()
{
// Your logic
if (MyPersonalizedUCEvent != null)
{
MyPersonalizedUCEvent("sample parameter");
}
}
More about the Action
delegate can be found in this link.
Note:
In many cases if events are not used properly they can cause memory leaks. Just make sure that you have written code to remove the registered event handlers as shown below.
MyPersonalizedUCEvent -= MyPersonalizedUCEventHandler;