4

I'm using a custom Attached Event that I created and I'm trying to add a handler to this event

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void dataGridTasks_Drop(object sender, RoutedEventArgs e)
    {

    }
}

Here the XAML code

<ListView  util:DragDropHelper.Drop="dataGridTasks_Drop">

I have this error in runtime at InitializeComponent

Object of type 'System.String' cannot be converted to type 'System.Windows.RoutedEventHandler'.

Anyone knows why i'm getting this error ? Thanks !

Here my Event code

    public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent(
        "Drop", RoutingStrategy.Bubble, typeof(DropEventArgs), typeof(DragDropHelper));

    public static void AddDropHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie != null)
        {
            uie.AddHandler(DragDropHelper.DropEvent, handler);
        }
    }

    public static void RemoveDropHandler(DependencyObject d, RoutedEventHandler handler)
    {
        UIElement uie = d as UIElement;
        if (uie != null)
        {
            uie.RemoveHandler(DragDropHelper.DropEvent, handler);
        }
    }

DropEventArgs code

class DropEventArgs : RoutedEventArgs
{
    public object Data { get; private set; }
    public int Index { get; private set; }

    public DropEventArgs(RoutedEvent routedEvent, object data, int index) 
        : base(routedEvent)
    {
        Data = data;
        Index = index;
    }
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Zied
  • 1,696
  • 12
  • 20
  • have you tried attaching the event handler in code instead of the designer? – Charlie Brown Jan 22 '10 at 23:30
  • I didn't tried, it might work in the code behind. But, why it doesn't work in the xaml code ? specially that all the examples that I found online works like that. – Zied Jan 23 '10 at 04:27
  • 1
    Can you please mention how you have defined the event? – mg007 Jan 23 '10 at 06:21
  • 1
    I second Mihir's question. Normally WPF would translate the string that describes the eventhandler to a method. Propbably you made a mistake in the code that registers the event. – Dabblernl Jan 23 '10 at 19:51
  • I had this exact mistake in my own code. There isn't a parameters object to pass to EventManager.RegisterRoutedEvent. (http://msdn.microsoft.com/en-us/library/system.windows.eventmanager.registerroutedevent.aspx) There should be. – twip May 23 '13 at 01:40

1 Answers1

6

After few hours checking the samples and my code, the problem was because of event definition of the Event indeed.(Thanks Mihir and Dabblernl).

I've done a mistake in the 3rd argument of RegisterRoutedEvent method by providing the event type instead of a Handler type.

The correct code is the following :

    public delegate void DropEventHandler(object sender, DropEventArgs e);

    public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent(
        "Drop", RoutingStrategy.Bubble, typeof(DropEventHandler), typeof(DragDropHelper));

The error message was misleading.

Zied
  • 1,696
  • 12
  • 20