如何使用动态创建的子元素中的路由事件?
我的主窗口中有一个事件作为路由事件从我的子控件之一触发。 MainWindow 有一个 AddHandler 调用来捕获路由的火。
我想从另一个子元素触发相同的事件,但该元素(menuItem)是动态创建的,因此当我尝试在 MainWindow 中使用 AddHandler 时,例如:
this.AddHandler(MyMenuItem.EditExtensionsEvent, new RoutedEventHandler(this.EditExtensions));
我收到 null 参数异常,因为 MyMenuItem 尚不存在。
有人知道我仍然可以使用路由事件的方法吗?
I have an event in my MainWindow that is being fired from one of my child controls as a routed event. The MainWindow has an AddHandler call to catch the routed fire.
I would like to fire this same event from ANOTHER child element, but this element (a menuItem) gets created on the fly so when I try to use AddHandler in MainWindow, like:
this.AddHandler(MyMenuItem.EditExtensionsEvent, new RoutedEventHandler(this.EditExtensions));
I get a null argument exception because MyMenuItem does not exist yet.
Anyone know of a way that I can still use a routed event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的 MyMenuItem 不在应用程序的命名空间中,或者 EditExtensionsEvent 不是 MyMenuItem 类的静态 RoutedEvent。
它应该看起来像这样:
请参阅 http://msdn.microsoft.com/en -us/library/ms752288.aspx
如果以这种方式声明,它应该像您在此处所示的那样工作
编辑:
我建议注册一个已经存在的事件,以确保您的 EditExtensionsEvent 正常工作。
I assume your MyMenuItem is either not in the namespace of your application or the EditExtensionsEvent isn't a static RoutedEvent of the class MyMenuItem.
It should look something like this:
see http://msdn.microsoft.com/en-us/library/ms752288.aspx
If it is declared this way it should work as you've shown here
EDIT:
I'd suggest registering with an already existing event to make sure your EditExtensionsEvent is working properly.