您好,我正在使用自定义的WF4活动,它可以由自己嵌套。我必须捕捉双击事件,所以我重写了OnPreviewMouseDoubleClick方法。我的问题是,当activity在另一个activity中,并且双击到内部活动时,在这两个activity上都会调用双击。我设置了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>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();
}
}
}发布于 2012-12-17 16:39:29
MSDN为您提供了答案:link
引用:尽管这个路由事件(Control.MouseDoubleClick Event)看起来像是沿着元素树的冒泡路径,但它实际上是一个由每个UIElement沿着元素树引发的直接路由事件。如果在MouseDoubleClick事件处理程序中将Handled属性设置为true,则路由上的后续MouseDoubleClick事件将在Handled设置为false的情况下发生。对于希望在用户双击控件时收到通知并希望在应用程序中处理该事件的控件使用者来说,这是一个较高级别的事件。
发布于 2012-12-18 04:02:41
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;
}
}
}发布于 2012-12-17 16:36:14
您是否尝试过对handled属性使用静态布尔值?我记得在拖放的时候也遇到过同样的问题,并以这种方式解决了它。
https://stackoverflow.com/questions/13910669
复制相似问题