4

作成したカスタム添付イベントを使用しており、このイベントにハンドラーを追加しようとしています

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

    private void dataGridTasks_Drop(object sender, RoutedEventArgs e)
    {

    }
}

ここに XAML コード

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

InitializeComponent で実行時にこのエラーが発生しました

'System.String' 型のオブジェクトは、'System.Windows.RoutedEventHandler' 型に変換できません。

このエラーが発生する理由を知っている人はいますか? ありがとう !

ここに私のイベントコード

    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 コード

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

1 に答える 1

6

サンプルと私のコードを数時間チェックした後、問題は実際にイベントのイベント定義が原因でした.(Mihir と Dabblernl に感謝します)。

RegisterRoutedEvent メソッドの 3 番目の引数で、ハンドラー タイプの代わりにイベント タイプを提供するというミスを犯しました。

正しいコードは次のとおりです。

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

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

エラー メッセージは誤解を招くものでした。

于 2010-01-25T17:33:12.607 に答える