C# WPF行为鼠标向下不';t火
我正在学习Microsoft.Expression.Interactions.WPF。我正在尝试将行为附加到网格的MouseDown事件。调用了C# WPF行为鼠标向下不';t火,c#,wpf,expression-blend-4,C#,Wpf,Expression Blend 4,我正在学习Microsoft.Expression.Interactions.WPF。我正在尝试将行为附加到网格的MouseDown事件。调用了OnAttached方法,但我的行为从未收到MouseDown事件 XAML C# 使用系统; 使用System.Windows; 使用System.Windows.Input; 使用System.Windows.Interactive; 使用System.Windows.Media; 命名空间SequenceDiagramViewer { 公共类鼠
OnAttached
方法,但我的行为从未收到MouseDown
事件
XAML
C#
使用系统;
使用System.Windows;
使用System.Windows.Input;
使用System.Windows.Interactive;
使用System.Windows.Media;
命名空间SequenceDiagramViewer
{
公共类鼠标单击:行为
{
受保护的覆盖无效附加()
{
Console.WriteLine(“附加调用”);
AssociatedObject.MouseDown+=AssociatedObject\u MouseDown;
}
私有void关联对象\u MouseDown(对象发送方,MouseButtonEventArgs e)
{
控制台。WriteLine(“称为MouseDown”);
}
附加时受保护的覆盖无效()
{
Console.WriteLine(“称为OnDetaching”);
AssociatedObject.MouseDown-=AssociatedObject\u MouseDown;
}
}
}
在我的控制台输出中,我只得到一个名为的附件。此外,当我在
AssociatedObject\u MouseDown
中放置断点时,它不会被命中,但onatached
会被命中。如何让事件进入我的行为?我的网格背景是透明的。一旦我将我的网格XAML更改为:
<Grid Background="White">
<interactions:Interaction.Behaviors>
<local:MouseClick />
</interactions:Interaction.Behaviors>
</Grid>
然后我开始记录鼠标按下事件
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
namespace SequenceDiagramViewer
{
public class MouseClick : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
Console.WriteLine("OnAttached called");
AssociatedObject.MouseDown += AssociatedObject_MouseDown;
}
private void AssociatedObject_MouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("MouseDown called");
}
protected override void OnDetaching()
{
Console.WriteLine("OnDetaching called");
AssociatedObject.MouseDown -= AssociatedObject_MouseDown;
}
}
}
<Grid Background="White">
<interactions:Interaction.Behaviors>
<local:MouseClick />
</interactions:Interaction.Behaviors>
</Grid>