开发者

Add multiple event handlers for one event in 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!


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜