你好我正在使用可以自己嵌套的自定义WF4活动。我必须抓住双击事件,所以我重写了OnPreviewMouseDoubleClick方法。我的问题是,当活动在其他活动中并且双击到内部活动时,会在两个活动上调用双击。我设置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();
}
}
}
答案 0 :(得分:1)
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;
}
}
}
答案 1 :(得分:0)
您是否尝试过使用静态bool处理属性? 我记得拖拽和掉落有同样的问题并以这种方式解决了。
答案 2 :(得分:0)
MSDN为您解答: link
Quote:虽然这个路由事件(Control.MouseDoubleClick Event
)似乎遵循通过元素树的冒泡路线,但它实际上是每个UIElement沿元素树引发的直接路由事件。如果在MouseDoubleClick事件处理程序中将Handled属性设置为true,则沿着路径的后续MouseDoubleClick事件将在Handled设置为false的情况下发生。对于希望在用户双击控件并在应用程序中处理事件时收到通知的控制使用者,这是一个更高级别的事件。