1

我有一个问题System.Windows.Interactivity.EventTriggerCustomControl当 my是某个标准 WPF 面板的孩子时,它可以完美运行,但是每当我将它放在 myCustomPanelControl中时,该Tap事件就永远不会被订阅。CustomPanelControl如果我将from的基类更改为FrameworkElementPanel则它可以工作。我假设我需要实现一些东西,但是什么?

自定义控件.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);
    }
}
4

1 回答 1

0

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

这篇文章被证明非常有用(A Brief Look at UIElementCollection section): http: //www.codeproject.com/Articles/24982/Conceptual-Children-A-powerful-new-concept-in-WPF

于 2013-04-09T07:15:25.157 回答