首页 > 解决方案 > 未在自定义控件中的 UI 元素上触发指针事件

问题描述

我正在创建一个UserControl包含多个UI Elements. 由于要渲染的数量取决于用户的输入,因此将其UserControl渲染为StackPanelusing 。ItemsControlUserControls

中的基本 XAMLUserControl如下所示。

<Grid x:Name="Viewport" VerticalAlignment="Top" HorizontalAlignment="Center">
    <Border x:Name="ViewportBorder" Background="White" BorderThickness="2, 2, 2, 2" BorderBrush="#FF353334" />
    <Image x:Name="Image" Margin="0" UseLayoutRounding="True" ManipulationMode="Scale"/>
    <InkCanvas x:Name="InkCanvas" />
    <Canvas x:Name="SelectionCanvas" CompositeMode="SourceOver" />
</Grid>

当用户将鼠标悬停在 上时,我想更改光标图标SelectionCanvas(基于我在源代码中看到的条件检查)。这看起来很简单,所以我尝试使用PointerEntered&PointerExited事件从SelectionCanvas. 并PointerMoved更改光标图标。但似乎没有任何事件被触发。

我也尝试绑定到Viewport网格元素,但也没有运气。

我不确定我在这里错过了什么。有人可以帮我吗?任何帮助深表感谢。请在此处找到完整的源代码。

标签: c#xamleventsuwpcustom-controls

解决方案


PointerEntered和事件被引发,PointerExited前提是应该引发它们的区域被绘制,因此尝试将 的Background属性设置Canvas为一些画笔,例如Transparent

<Canvas x:Name="SelectionCanvas" CompositeMode="SourceOver"             
        Background="Transparent"             
        PointerEntered="SelectionCanvas_PointerEntered"
        ...

推荐阅读