[英]Adding event to button programmatically
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
toSystem.Windows.RoutedEventhandler
无法将类型
System.EventHandler
隐式转换为System.Windows.RoutedEventhandler
The onChangeActivityFilter methode looks like this: onChangeActivityFilter方法看起来像这样:
private void onChangeActivityFilter(object sender, EventArgs e)
{
}
I'd like to know what I'm doing wrong. 我想知道我在做什么错。
You need to create a instance of RoutedEventHandler
: 您需要创建一个
RoutedEventHandler
的实例:
activityButton.Click += new RoutedEventhandler(onChangeActivityFilter);
And also change the method signature: 并更改方法签名:
private void onChangeActivityFilter(object sender, RoutedEventArgs e)
{
}
RoutedEvents where introduced with WPF. WPF引入的RoutedEvents。
You can also use lambda functions 您还可以使用lambda函数
activityButton.Click += (sender, e) =>
{
MessageBox.Show("the button was clicked");
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.