in procedural code in can do the following:
// Add two event handler for the button click event
button1.Click += new RoutedEventHandler(button1_Click_1);
button1.Click += new RoutedEventHandler(button1_Click_2);
But how can I add multiple event handlers for the button's click event in XAML? Thanks for any hint!
You cannot subscribe more than one event handler in XAML. You can however achieve the same effect by subscribing a single event handler and then calling two or more methods from the event handler.
private void Button_OnClick(object sender, RoutedEventArgs e)
{
ButtonOnClick1();
ButtonOnClick2();
}
private void ButtonOnClick1()
{
//Do something...
}
private void ButtonOnClick2()
{
//Do something...
}
You can specify multiple handlers in xaml
like this :
<Style TargetType="{x:Type Button}">
<EventSetter Event="Click" Handler="ChangeBackground1"/>
<EventSetter Event="Click" Handler="ChangeBackground2"/>
<EventSetter Event="Click" Handler="ChangeBackground3"/>
<EventSetter Event="Click" Handler="ChangeBackground4"/>
</Style>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With