【问题标题】:Add MouseLeftButtonUp events dynamically动态添加 MouseLeftButtonUp 事件
【发布时间】:2014-01-04 19:40:08
【问题描述】:

我在 MainWindow.xml 中添加了以下 2 个方法,但我无法触发事件

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    lstContacts.Items.Clear();
    cvMenuItem test = new cvMenuItem("test", Environment.GetEnvironmentVariable("USERPROFILE") + @"\Downloads\images.jpg");
    test.MouseLeftButtonUp += new System.EventHandler(imClicked);
    lstContacts.Items.Add(test);
}

void imClicked(object sender, EventArgs e)
{
    MessageBox.Show("hello");
}

我一直收到这个错误,为什么?

无法将类型“System.EventHandler”隐式转换为 'System.Windows.Input.MouseButtonEventHandler' MainWindow.xaml.cs 64 39 WpfApplication1

【问题讨论】:

  • 如错误消息所述,您正在尝试将 System.EventHandler 添加到 MouseLeftButtonUp 委托,而您应添加 MouseButtonEventHandler 其中委托应返回 void 并首先参数为object,第二个为MouseButtonEventArgs

标签: c# wpf events add


【解决方案1】:

试试这个

test.MouseLeftButtonUp += imClicked;


private void imClicked(object sender, MouseButtonEventArgs e)
    {
             MessageBox.Show("hello");
    }

【讨论】:

    猜你喜欢
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多