Handler for a custom attached event

I am using a custom attached event that I created and I am 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 is the XAML code

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

I have this runtime error in InitializeComponent

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

Does anyone know why I am getting this error? Thanks!

Here is 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; } } 
+4
source share
1 answer

After hours of checking the samples and my code, the problem was caused by determining the event event really. (Thanks to Mihir and Dabblernl).

I made a mistake in the third argument of the RegisterRoutedEvent method, indicating the type of event instead of the type of handler.

The correct code is as follows:

  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 is misleading.

+6
source

Source: https://habr.com/ru/post/1299156/


All Articles