Pregunta

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();
        }
    }
}
¿Fue útil?

Solución 2

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.

Otros consejos

Remark in Makus link continues like this:

Control authors who want to handle mouse double clicks should use the MouseLeftButtonDown event when ClickCount is equal to two. This will cause the state of Handled to propagate appropriately in the case where another element in the element tree handles the event.

So I created this workaround:

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

Have you tried using a static bool for the handled property? I remember having a same issue with drag&drop and solved it this way.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top