简体   繁体   中英

pass method to event handler in C# WPF

This works in adding an event handler in C# WPF

CheckBox ifPrint = new CheckBox();
ifPrint.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler(
(sender, e) => //toggle check box event
{
    //do stuff
}));

but it looks messy when the method body gets long, so I want to define the method elsewhere like this

ifPrint.AddHandler(CheckBox.ClickEvent, delegate(object sender, RoutedEventArgs e){
    checkBoxClick(sender, e);
});

private void checkBoxClick(object sender, RoutedEventArgs e)
{
//do stuff
}

but this doesn't even compile with the error: Cannot convert anonymous type to type 'System.Delegate' because it is not a delegate type

Sorry, I am new to this and have no idea how it's supposed to be done. Is this even close? Thanks!

You can subscribe to a separate method like this, as long as the signature of checkBoxClick is correct:

ifPrint.Click += checkBoxClick;

You can also subscribe to an event inline like this:

ifPrint.Click += (s, e) => SomeMethod();

Which then allows you to name your method something more reasonable and not require it to accept parameters:

private void SomeMethod()
{
    //do stuff
}

Just to explain it a little further, in the above code, s and e take the place of the parameters in your checkBoxClick event method, so it's basically equivalent to this:

ifPrint.Click += checkBoxClick;

private void checkBoxClick(object sender, RoutedEventArgs e)
{
    SomeMethod();
}

Edit, in regards to your comment.

Given this is much simpler, when, if ever, should one use this? ifPrint.AddHandler(CheckBox.ClickEvent, new RoutedEventHandler( (sender, e) => { //do stuff }));

I honestly don't think I've ever used that syntax.

It seems that in most cases it does the same thing . According to the MSDN docs, there's a handledEventsToo parameter on the AddHandler() method, which I think could be significant.

Imagine you subscribed to an event multiple times, like this:

ifPrint.Click += checkBoxClick;
ifPrint.Click += checkBoxClick;
ifPrint.Click += checkBoxClick;

And inside your event, you set e.Handled = true . If you didn't have that line, you'd see the message box displayed 3 times. But with that line, you only get the message box once, because the first time the event fires, it marks the event "handled".

private void checkBoxClick(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Clicked!");

    e.Handled = true;
}

By passing in true for the last parameter (it's false by default), you actually tell it to fire that event, even if other events already "handled" the event .

ifPrint.AddHandler(CheckBox.ClickEvent,
    new RoutedEventHandler((s, e) => { /* do stuff */ }), true);

try this logic to attach click event handler for your checkbox.

CheckBox ifPrint = new CheckBox();
ifPrint.Click+=checkBoxClick;


private void checkBoxClick(object sender, RoutedEventArgs e)
{
//do stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM