简体   繁体   中英

WPF Behavior MouseDown Doesn't Fire

I'm learning Microsoft.Expression.Interactions.WPF. I'm attempting to attach a behavior to a grid's MouseDown event. The OnAttached method gets called, but my behavior never receives the MouseDown event.

XAML

<Window x:Class="SequenceDiagramViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SequenceDiagramViewer"
        xmlns:ineractivity="http://schemas.microsoft.com/expression/2010/interactivity"
        mc:Ignorable="d"
        Title="Sequence Diagram Viewer" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <Grid>
        <ineractivity:Interaction.Behaviors>
            <local:MouseClick />
        </ineractivity:Interaction.Behaviors>
    </Grid>
</Window>

C#

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

In my console output, I only get OnAttached called . In addition, when I put a breakpoint in AssociatedObject_MouseDown , it never gets hit, but OnAttached does. How do I get the event to go to my behavior?

My grid background was transparent. As soon as I changed my grid XAML to this:

<Grid Background="White">
    <interactions:Interaction.Behaviors>
        <local:MouseClick />
    </interactions:Interaction.Behaviors>
</Grid>

I then started getting mouse down events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM