简体   繁体   English

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

[英]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属性正常工作,必须重写GetVisualChildVisualChildrenCount

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.

相关问题 背后的代码中的System.Windows.Interactivity.EventTrigger在某些控件上有效,但在其他控件上则无效 - System.Windows.Interactivity.EventTrigger in codebehind works on some Controls, but not others 使用System.Windows.Interactivity.EventTrigger的ViewModel中的事件是弱引用吗? - Events in ViewModel with System.Windows.Interactivity.EventTrigger, is it weak referencing? 通过 Interactivity.EventTrigger 捕获自定义事件 - Catch custom event by Interactivity.EventTrigger 如何使用Interactivity.EventTrigger捕获自定义事件 - How to catch a custom event with Interactivity.EventTrigger System.Windows.Interactivity DLL计数 - System.Windows.Interactivity DLL Count 如何在项目中添加System.Windows.Interactivity? - How to add System.Windows.Interactivity to project? System.Windows.Interactivity.dll加载两次 - System.Windows.Interactivity.dll loaded twice System.Windows.TriggerAction与System.Windows.Interactivity.TriggerAction - System.Windows.TriggerAction vs System.Windows.Interactivity.TriggerAction 如何使用System.Windows.Interactivity Windows Phone 8.1 - How to use System.Windows.Interactivity windows phone 8.1 棱镜组件参考失败:System.Windows.Interactivity - Prism assembly reference failure: System.Windows.Interactivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM