Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple event handlers for one event in XAML?

Tags:

c#

events

wpf

xaml

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!

like image 865
stefan.at.wpf Avatar asked Aug 01 '10 20:08

stefan.at.wpf


2 Answers

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...
    }
like image 50
Tim Lloyd Avatar answered Oct 13 '22 00:10

Tim Lloyd


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>
like image 34
AnjumSKhan Avatar answered Oct 13 '22 00:10

AnjumSKhan