自定义附加事件的处理程序
我正在使用我创建的自定义附加事件,并且我正在尝试向此事件添加处理程序
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.RoatedEventHandler'。
有人知道为什么我会收到此错误吗? 谢谢 !
这是我的事件代码
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;
}
}
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几个小时检查示例和我的代码后,问题确实是由于事件的事件定义造成的。(感谢 Mihir 和 Dabblernl)。
我在 RegisterRoatedEvent 方法的第三个参数中犯了一个错误,提供了事件类型而不是处理程序类型。
正确的代码如下:
错误消息具有误导性。
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 :
The error message was misleading.