2010-01-22 1 views
4
내가 만든 사용자 지정 연결된 이벤트를 사용하고 있는데 내가 여기에이 이벤트

처리기 정의 연결된 이벤트

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

    private void dataGridTasks_Drop(object sender, RoutedEventArgs e) 
    { 

    } 
} 

XAML 코드

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

I에 처리기를 추가하기 위해 노력하고있어

에 대한 런타임시 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; 
    } 
} 
+0

디자이너 대신 코드에서 이벤트 처리기를 연결해 보았습니까? –

+0

시도하지 않았지만 코드 뒤에 작동 할 수도 있습니다. 하지만 xaml 코드에서 작동하지 않는 이유는 무엇입니까? 특별히 내가 온라인에서 찾은 모든 예가 그렇게 작동한다는 것을 알 수 있습니다. – Zied

+1

이벤트 정의 방법을 말씀해 주시겠습니까? – mg007

답변

6

. (감사합니다 미 히어 및 Dabblernl).

Handler 유형 대신 이벤트 유형을 제공하여 RegisterRoutedEvent 메소드의 3 번째 인수에서 실수를했습니다.

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

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

오류 메시지는 오해의 소지가 있었다 :

올바른 코드는 다음과 같다.