WPF Interaction.Triggers MouseDown EventTrigger 不适用于 Canvas 元素

WPF Interaction.Triggers MouseDown EventTrigger Not Working with Canvas Element

我正在尝试创建一个允许我在 window 中绘制矩形的 MVVM WPF 应用程序。现在,我创建了一个覆盖整个屏幕的 canvas 元素。按照其他问题和指南,看起来在鼠标事件(例如 MouseDownMouseUp 等)上使用命令绑定的唯一方法是使用 System.Windows.Interactivity 程序集并添加 EventTriggers鼠标事件。但是,这似乎不适用于 canvas 元素。单击 click/hold、click/drag、右键单击 none 会触发我的命令的 Execute 方法。

canvas 元素支持哪些事件?为什么 MouseDownPreviewMouseDown 不起作用?

XAML 为 window:

<Window x:Class="RutheniumReader.SnippingWindow.SnippingShade"
    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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:snippingWindow="clr-namespace:MyApp.SnippingWindow"
    mc:Ignorable="d"
    Title="Snipping Window"
    WindowState="Maximized"
    Background="Black"
    >
    <Window.DataContext>
        <snippingWindow:SnippingWindowViewModel />
    </Window.DataContext>
    <Canvas x:Name="Canvas">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDown">
                <i:InvokeCommandAction Command="{Binding Path=MouseDownCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Canvas>
</Window>

出于测试目的,我尝试了以下事件但没有成功:

鼠标事件不起作用,因为 canvas 的背景未设置。 如果控件(在本例中是您的 Canvas)没有设置背景颜色,背景颜色将默认为空,这会使控件无法进行命中测试。

背景可以显式设置或通过样式设置,只要它的值不同于 null(甚至 'Transparent'),它就应该拾取鼠标事件。