WPF - 处理自定义控件上的自定义附加事件
我有一个这样声明的路由事件(名称已更改以保护无辜者):
public class DragHelper : DependencyObject {
public static readonly RoutedEvent DragCompleteEvent = EventManager.RegisterRoutedEvent(
"DragComplete",
RoutingStrategy.Bubble,
typeof(DragRoutedEventHandler),
typeof(DragHelper)
);
public static void AddDragCompleteHandler( DependencyObject dependencyObject, DragRoutedEventHandler handler ) {
UIElement element = dependencyObject as UIElement;
if (element != null) {
element.AddHandler(DragCompleteEvent, handler);
}
}
public static void RemoveDragCompleteHandler( DependencyObject dependencyObject, DragRoutedEventHandler handler ) {
UIElement element = dependencyObject as UIElement;
if (element != null) {
element.RemoveHandler(DragCompleteEvent, handler);
}
}
非常标准的东西。在 XAML 中,我有一个包含单个自定义控件的 DataTemplate。我试图将此事件(以及其他一些附加属性)附加到控件:
<DataTemplate ...>
<My:CustomControl
My:DragHelper.IsDragSource="True"
My:DragHelper.DragComplete="DragCompleteHandler" />
</DataTemplate>
这无法产生所需的结果。具体来说,当调用为 DragComplete 事件调用 RaiseEvent() 的代码时,绝不会调用处理程序。事实上,也不是连接到此 XAML 文件中其他位置的任何其他自定义路由事件的处理程序。
我尝试更改路由事件的名称,并尝试将数据模板从具有 DataType 的模板切换为具有 x:Key 的模板。这不会对行为产生明显的变化。
但是,如果我将 My:CustomControl 更改为任何内置 WPF 控件(例如 TextBlock),则事件将完全按照我的预期触发。同样,如果我将自定义控件替换为项目中的任何其他自定义 UserControl 子类,则行为将恢复为损坏的、似乎没有事件可处理的状态。
这对我来说没有多大意义。为了让这个场景发挥作用,我需要做一些具体的事情吗?看来应该没什么关系。我认为我在所有自定义控件中所做的特定操作可能会导致事件处理中断,但到目前为止,我在尝试过的三四个自定义控件中还没有看到任何常见的情况。
I have a routed event declared as such (names have been changed to protect the innocent):
public class DragHelper : DependencyObject {
public static readonly RoutedEvent DragCompleteEvent = EventManager.RegisterRoutedEvent(
"DragComplete",
RoutingStrategy.Bubble,
typeof(DragRoutedEventHandler),
typeof(DragHelper)
);
public static void AddDragCompleteHandler( DependencyObject dependencyObject, DragRoutedEventHandler handler ) {
UIElement element = dependencyObject as UIElement;
if (element != null) {
element.AddHandler(DragCompleteEvent, handler);
}
}
public static void RemoveDragCompleteHandler( DependencyObject dependencyObject, DragRoutedEventHandler handler ) {
UIElement element = dependencyObject as UIElement;
if (element != null) {
element.RemoveHandler(DragCompleteEvent, handler);
}
}
Pretty standard stuff. In XAML, I have a DataTemplate that contains a single custom control. I am attempting to attach this event (as well as some other attached properties) to the control:
<DataTemplate ...>
<My:CustomControl
My:DragHelper.IsDragSource="True"
My:DragHelper.DragComplete="DragCompleteHandler" />
</DataTemplate>
This fails to produce the desired results. Specifically, while the code that calls RaiseEvent() for the DragComplete event is called, the handler is never invoked. Nor, in fact, are the handlers for any other custom routed events that are hooked up elsewhere in this XAML file.
I tried changing the name of the routed event, and I tried switching the data template from one with a DataType to one with an x:Key. This produced no visible change in the behavior.
However, if I change My:CustomControl to any built-in WPF control, such as a TextBlock, the events are fired exactly as I would exect. Similarly, if I replace my custom control with any other custom UserControl subclass from my project, the behavior reverts to the broken, no-events-ever-seem-to-get-handled state.
This isn't make a whole lot of sense to me. Is there something specific I need to do to get this scenario to work? It seems like it should not matter. I suppose it's possible there is a specific thing I've done in all my custom controls that causes the event handling to break, but I haven't seen anything common in the three or four custom controls I've tried so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有发布所有代码,因此我必须推断并组合我自己的版本。对我来说效果很好。也许与您的代码进行比较和对比:
Window1.xaml.cs:
Window1.xaml:
You haven't posted all your code, so I had to infer and put together my own version. It works OK for me. Perhaps compare and contrast to your code:
Window1.xaml.cs:
Window1.xaml: