[英]System.Windows.Interactivity.EventTrigger and custom controls with children
I have a problem with System.Windows.Interactivity.EventTrigger
. 我对System.Windows.Interactivity.EventTrigger
有问题。 It works perfectly when my CustomControl
is a child of some standard WPF panel, but whenever I put it inside my CustomPanelControl
, the Tap
event is never subscribed to. 当我的CustomControl
是某些标准WPF面板的子级时,它可以完美工作,但是每当我将它放在CustomPanelControl
,就不会订阅Tap
事件。 If I change the base class of CustomPanelControl
from FrameworkElement
to Panel
then it works. 如果我将CustomPanelControl
的基类从FrameworkElement
更改为Panel
那么它将起作用。 I'm assuming there is something I need to implement, but what? 我假设有一些东西需要实现,但是呢?
CustomControl.cs: CustomControl.cs:
public class CustomControl: FrameworkElement
{
public void RaiseTap()
{
OnTap();
}
protected virtual void OnTap()
{
RaiseEvent(new RoutedEventArgs(TapEvent));
}
public static readonly RoutedEvent TapEvent = EventManager.RegisterRoutedEvent("Tap", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomControl));
public event RoutedEventHandler Tap
{
add { AddHandler(TapEvent, value); }
remove { RemoveHandler(TapEvent, value); }
}
}
CustomPanelControl.cs: CustomPanelControl.cs:
[ContentProperty("Children")]
public class CustomPanelControl: FrameworkElement
{
public UIElementCollection Children { get; private set; }
public CustomPanelControl()
{
Children = new UIElementCollection(this, this);
}
}
I have found the solution. 我找到了解决方案。 For Children
property to work GetVisualChild
and VisualChildrenCount
need to be overridden. 为了使Children
属性正常工作,必须重写GetVisualChild
和VisualChildrenCount
。
This article proved to be very useful ( A Brief Look at UIElementCollection section): http://www.codeproject.com/Articles/24982/Conceptual-Children-A-powerful-new-concept-in-WPF 本文被证明是非常有用的( “ UIElementCollection的简要介绍”部分): http : //www.codeproject.com/Articles/24982/Conceptual-Children-A-powerful-new-concept-in-WPF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.