我对System.Windows.Interactivity.EventTrigger
有疑问。当CustomControl
是某个标准WPF面板的子项时,它可以正常工作,但每当我将其放入CustomPanelControl
时,Tap
事件永远不会被订阅。如果我将CustomPanelControl
的基类从FrameworkElement
更改为Panel
,那么它就可以了。我假设我需要实现一些东西,但是什么呢?
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:
[ContentProperty("Children")]
public class CustomPanelControl: FrameworkElement
{
public UIElementCollection Children { get; private set; }
public CustomPanelControl()
{
Children = new UIElementCollection(this, this);
}
}
答案 0 :(得分:0)
我找到了解决方案。要使Children
属性工作GetVisualChild
,需要覆盖VisualChildrenCount
。
本文证明非常有用(简要介绍UIElementCollection 部分): http://www.codeproject.com/Articles/24982/Conceptual-Children-A-powerful-new-concept-in-WPF