0

I want to add an event to a programmatically generated button like this:

Button activityButton = new Button();
activityButton.Click += new EventHandler(onChangeActivityFilter);

I'm getting the following exception in the 2nd line:

Cannot implicit convert type System.EventHandler to System.Windows.RoutedEventhandler

The onChangeActivityFilter methode looks like this:

private void onChangeActivityFilter(object sender, EventArgs e)
{

}

I'd like to know what I'm doing wrong.

0

2 Answers 2

5

You need to create a instance of RoutedEventHandler:

activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);

And also change the method signature:

private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{

}

RoutedEvents where introduced with WPF.

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use lambda functions

activityButton.Click += (sender, e) => 
{
    MessageBox.Show("the button was clicked");
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.