vcmkrtchyan
vcmkrtchyan

Reputation: 2626

How to attach 2 methods to button click in WPF XAML?

I have two methods that need to be called when a button is pressed.

I can do it by code in this way

this.button1.Click += new System.EventHandler(this.button_Click);
this.button1.Click += new System.EventHandler(this.button_Click_1);

but how can I do it in XAML?

Upvotes: 1

Views: 464

Answers (1)

Mike Eason
Mike Eason

Reputation: 9713

Assuming your form is one of your own, meaning you can change it.

Consider using a virtual method.

public class MyForm
{
    public virtual void DoSomething()
    {
        //Some code.
        System.Diagnostics.Debug.WriteLine("Hello from MyForm");
    }
}

public class MyOtherForm : MyForm
{
    public override void DoSomething()
    {
        //Call the base DoSomething first
        base.DoSomething();

        //Some code to run after.
        System.Diagnostics.Debug.WriteLine("Hello from MyOtherForm");
    }
}

If you want to achieve this in XAML. See here as it has already been answered.

Upvotes: 1

Related Questions