Search code examples
c#xamlworkflow-foundation-4workflow-activityactivitydesigner

Custom WF4 Activity: Why e.Handled = true in DoubleClick don't stops bubbling


Hallo I'm using custom WF4 activity which can be nested by self. I have to catch a double-click event, so I rewrote OnPreviewMouseDoubleClick method. My problem is that when activity is inside the other activity and am the double click to inner one, the double click is invoked on both of them. I set the e.Handled = true but it doesn't work. How can I stop to execute the double click event on parent activities.

Here is my example of codes:

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>

ActivityDesigner1.xaml.cs

using System.Windows;
using System.Windows.Input;

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

        protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
        {
            e.Handled = true;
            base.OnPreviewMouseDoubleClick(e);
            MessageBox.Show(this.GetHashCode().ToString());   
        }
    }
}

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();
        }
    }
}

Solution

  • MSDN has answer for you: link

    Quote: Although this routed event (Control.MouseDoubleClick Event) seems to follow a bubbling route through an element tree, it actually is a direct routed event that is raised along the element tree by each UIElement. If you set the Handled property to true in a MouseDoubleClick event handler, subsequent MouseDoubleClick events along the route will occur with Handled set to false. This is a higher-level event for control consumers who want to be notified when the user double-clicks the control and to handle the event in an application.