C# 自定义WF4活动:双击don';中为什么e.Handled=true;t停止冒泡

C# 自定义WF4活动:双击don';中为什么e.Handled=true;t停止冒泡,c#,xaml,workflow-foundation-4,workflow-activity,activitydesigner,C#,Xaml,Workflow Foundation 4,Workflow Activity,Activitydesigner,你好,我正在使用自定义的WF4活动,它可以自己嵌套。我必须捕获双击事件,所以我重写了onPreviewMouseBoolClick方法。我的问题是,当活动位于另一个活动内部,并且是对内部活动的双击时,会对这两个活动调用双击。我将e.Handled设置为true,但它不起作用。如何停止对父活动执行双击事件 以下是我的代码示例: ActivityDesigner1.xaml <sap:ActivityDesigner x:Class="ActivityDesignerLibrary1.Acti

你好,我正在使用自定义的WF4活动,它可以自己嵌套。我必须捕获双击事件,所以我重写了onPreviewMouseBoolClick方法。我的问题是,当活动位于另一个活动内部,并且是对内部活动的双击时,会对这两个活动调用双击。我将e.Handled设置为true,但它不起作用。如何停止对父活动执行双击事件

以下是我的代码示例:

ActivityDesigner1.xaml

<sap:ActivityDesigner x:Class="ActivityDesignerLibrary1.ActivityDesigner1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">
    <Grid>
        <sap:WorkflowItemsPresenter Items="{Binding Path=ModelItem.Activities}">
            <sap:WorkflowItemsPresenter.SpacerTemplate>
                <DataTemplate>
                    <Label HorizontalAlignment="Center" Content="Drop activity here." FontStyle="Italic" Foreground="DarkGray" />
                </DataTemplate>
            </sap:WorkflowItemsPresenter.SpacerTemplate>
        </sap:WorkflowItemsPresenter>
    </Grid>
</sap:ActivityDesigner>
CodeActivity1.cs

using System;
using System.Activities;
using System.Activities.Statements;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace ActivityDesignerLibrary1
{
    [Designer(typeof(ActivityDesigner1))]
    public sealed class CodeActivity1 : CodeActivity
    {
        private Sequence innerSequence = new Sequence();

        public Collection<Activity> Activities
        {
            get
            {
                return this.innerSequence.Activities;
            }
        }

        protected override void Execute(CodeActivityContext context)
        {
            throw new NotImplementedException();
        }
    }
}
使用系统;
使用系统活动;
使用系统、活动、报表;
使用System.Collections.ObjectModel;
使用系统组件模型;
命名空间ActivityDesignerLibrary1
{
[设计人(类型(活动设计人1))]
公共密封类CodeActivity 1:CodeActivity
{
私有序列innerSequence=新序列();
公众募捐活动
{
得到
{
返回this.innerSequence.Activities;
}
}
受保护的覆盖无效执行(CodeActivityContext上下文)
{
抛出新的NotImplementedException();
}
}
}

您是否尝试对已处理属性使用静态布尔值? 我记得我在拖放方面遇到了同样的问题,并通过这种方式解决了它。

MSDN为您提供了答案:


Quote:尽管此路由事件(
Control.MouseDoubleClick event
)似乎遵循通过元素树的冒泡路由,但它实际上是每个UIElement沿元素树引发的直接路由事件。如果在MouseDoubleClick事件处理程序中将Handled属性设置为true,则沿途发生的后续MouseDoubleClick事件将在Handled设置为false时发生。对于希望在用户双击控件时收到通知并在应用程序中处理事件的控件使用者,这是一个更高级别的事件

Makus链接中的评论如下:

当ClickCount等于2时,希望处理鼠标双击的控件作者应使用MouseLeftButtonDown事件。当元素树中的另一个元素处理事件时,这将导致Handled的状态适当传播

因此,我创建了这个变通方法:

using System.Windows;
using System.Windows.Input;
using System.Activities.Presentation;
using System.Windows.Media;

namespace ActivityDesignerLibrary1
{
    public partial class ActivityDesigner1
    {
        public ActivityDesigner1()
        {
            InitializeComponent();
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                FrameworkElement fe = e.OriginalSource as FrameworkElement;
                if (fe != null)
                {
                    object original = fe.DataContext;
                    ActivityDesigner baseActivityDesigner = original as ActivityDesigner;
                    if (baseActivityDesigner == null)
                    {
                        baseActivityDesigner = this.ActivityDesignerFinder((DependencyObject)e.OriginalSource);
                    }

                    if (baseActivityDesigner != null)
                    {
                        MessageBox.Show(baseActivityDesigner.GetHashCode().ToString());
                        e.Handled = true;
                    }
                }
            }
        }


        private ActivityDesigner ActivityDesignerFinder(DependencyObject dependencyObject)
        {
            while (dependencyObject != null)
            {
                if (dependencyObject is ActivityDesigner)
                {
                    return (ActivityDesigner)dependencyObject;
                }

                dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
            }

            return null;
        }
    }
}

是的,将属性e.Handled设置为true在拖放中效果很好,但在双击中效果不好。就像我说的:我在拖放中遇到了同样的问题,并使用静态属性修复了它。删除
base.onPreviewMouseBoolClick(e)并重试。谢谢,我刚刚删除了它,但仍然无法正常工作。谢谢您的链接非常有用。
using System.Windows;
using System.Windows.Input;
using System.Activities.Presentation;
using System.Windows.Media;

namespace ActivityDesignerLibrary1
{
    public partial class ActivityDesigner1
    {
        public ActivityDesigner1()
        {
            InitializeComponent();
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                FrameworkElement fe = e.OriginalSource as FrameworkElement;
                if (fe != null)
                {
                    object original = fe.DataContext;
                    ActivityDesigner baseActivityDesigner = original as ActivityDesigner;
                    if (baseActivityDesigner == null)
                    {
                        baseActivityDesigner = this.ActivityDesignerFinder((DependencyObject)e.OriginalSource);
                    }

                    if (baseActivityDesigner != null)
                    {
                        MessageBox.Show(baseActivityDesigner.GetHashCode().ToString());
                        e.Handled = true;
                    }
                }
            }
        }


        private ActivityDesigner ActivityDesignerFinder(DependencyObject dependencyObject)
        {
            while (dependencyObject != null)
            {
                if (dependencyObject is ActivityDesigner)
                {
                    return (ActivityDesigner)dependencyObject;
                }

                dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
            }

            return null;
        }
    }
}