如何使用委托类型解决方法绑定问题?

时间:2016-01-28 04:06:37

标签: c# wpf

我试图创建自己的ContextMenuOpening事件(Event ContextMenuOpening名称应该与FrameworkElement类相同)当我尝试从Xaml页面触发该事件时抛出“无法绑定到目标方法,因为它的签名或安全透明度不是与委托类型的兼容“但是当我在代码中挂钩事件时,它的工作正常。

请找到以下代码片段来重现问题并提供相关建议。

XAML

 <local:ContextMenuEvetTest  x:Name="menu"  ContextMenuOpening="menu_ContextMenuOpening" />

背后的代码

 #region Main Class
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        //this.menu.ContextMenuOpening += menu_ContextMenuOpening;
    }

    void menu_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
    }
} 
#endregion

#region Base Class

public class ContextMenuEvet : Control
{
    public ContextMenuEvet()
    {

    }
    public delegate void ContextMenuOpeningEventHandler(object sender, ContextMenuOpeningEventArgs e);

    public new event ContextMenuOpeningEventHandler ContextMenuOpening;

    internal void GetContextMenuOpeningEvent(ContextMenuOpeningEventArgs e)
    {
        if (ContextMenuOpening != null)
            ContextMenuOpening(this, e);
    }

} 
#endregion

#region Derived Class

public class ContextMenuEvetTest : ContextMenuEvet
{
    static ContextMenuEvetTest()
    {

    }
} 
#endregion

#region EventsArgument

public class ContextMenuOpeningEventArgs : CancelEventArgs
{

}
#endregion

提前致谢。

Jeyasri M

1 个答案:

答案 0 :(得分:0)

将您的活动定义更改为:

public new event ContextMenuEventHandler ContextMenuOpening
        {
            add
            {
                base.AddHandler(FrameworkElement.ContextMenuOpeningEvent, value);
            }
            remove
            {
                base.RemoveHandler(FrameworkElement.ContextMenuOpeningEvent, value);
            }
        }

并删除它,

if (ContextMenuOpening != null)
            ContextMenuOpening(this, e);

并使用,

internal void GetContextMenuOpeningEvent(ContextMenuEventArgs e)
    {
        base.OnContextMenuOpening(e);
    }