The OnClick is not belonged to UserControl Methods, you can override any UserControl Methods on your needs. In my test, I override the Method OnMouseLeftButtonDown
to raise the RaiseTapEvent for UserControl, below is my edited MyButtonSimple.cs
public class MyButtonSimple:UserControl
{
// Create a custom routed event by first registering a RoutedEventID
// This event uses the bubbling routing strategy
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent(
"Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButtonSimple));
// Provide CLR accessors for the event
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
}
// This method raises the Tap event
void RaiseTapEvent()
{
RoutedEventArgs newEventArgs = new RoutedEventArgs(MyButtonSimple.TapEvent);
RaiseEvent(newEventArgs);
}
// For demonstration purposes we raise the event when the MyButtonSimple is clicked
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
RaiseTapEvent();
}
}
To use it in MainWindow.xaml:
<local:MyButtonSimple Width="180" Height="30" Background="Aqua" Tap="MyButtonSample_Tab" ></local:MyButtonSimple>
The MainWindow.xaml.cs code is:
private void MyButtonSample_Tab(object sender, RoutedEventArgs e)
{
MessageBox.Show("This is MyButtonSimple:UserControl.");
}
When I clicked the MyButtonSimple, the result picture is:
This is my understanding for youe question, if I misunderstand, please point out and descibe your question in details which will beneficial for me to analyze.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.