Click here to Skip to main content
15,071,430 members
Please Sign up or sign in to vote.
5.00/5 (3 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?
Posted

1 solution

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

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


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

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


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


4) Main window code behind:
C#
private void OnCustomButtonClick(object sender, RoutedEventArgs e)
{
   //do something ...
}
   
Comments
Member 14134896 31-Jan-19 13:37pm
   
this is the best example. all people learn fast

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