System.Windows.Interactivity.EventTrigger和带子项的自定义控件

时间:2013-04-08 14:47:41

标签: c# .net wpf expression-blend

我对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);
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。要使Children属性工作GetVisualChild,需要覆盖VisualChildrenCount

本文证明非常有用(简要介绍UIElementCollection 部分): http://www.codeproject.com/Articles/24982/Conceptual-Children-A-powerful-new-concept-in-WPF