Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
5.00/5 (4 votes)
See more:
Hi everybody, I have a simple UserControl and a Button on it. How can i define a custom click event for my control so that when i use this user contol in the application, clicking the button, raise my custom event?

1 solution

I've found a solution, not sure it's the best answer though.

1) User control Xaml:
<MyButton Click="OnButtonClick">

2) User control code behind:
public event RoutedEventHandler CustomClick;

private void OnButtonClick( object sender, RoutedEventArgs e )
    if( CustomClick != null )
        CustomClick(this, new RoutedEventArgs());

3) Main window Xaml:
<MyCustomControl CustomClick = "OnCustomButtonClick">

4) Main window code behind:
private void OnCustomButtonClick(object sender, RoutedEventArgs e)
   //do something ...
Share this answer
Member 14134896 31-Jan-19 13:37pm    
this is the best example. all people learn fast
cRo13 17-Mar-23 9:36am    
thank you for this easy to use workflow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900