C# usercontrol中图像的MouseDown事件未触发
我在WPF中创建了一个带有图像的用户控件。我为此图像声明了一个C# usercontrol中图像的MouseDown事件未触发,c#,wpf,events,routed-events,C#,Wpf,Events,Routed Events,我在WPF中创建了一个带有图像的用户控件。我为此图像声明了一个MouseDown事件: <Image x:Name="imgState" Height="300" Width="300" MouseDown="imgState_MouseDown" OpacityMask="#00000000" /> 更新2 这是我的整个XAML文件: <UserControl x:Class="TicTacToe.controls.SingleField" xml
MouseDown
事件:
<Image x:Name="imgState" Height="300" Width="300" MouseDown="imgState_MouseDown" OpacityMask="#00000000" />
更新2
这是我的整个XAML文件:
<UserControl x:Class="TicTacToe.controls.SingleField"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image x:Name="imgState" MouseDown="imgState_MouseDown" Height="300" Width="300" Stretch="None" OpacityMask="#00000000"/>
</Grid>
</UserControl>
我再次删除了源代码,因为我在运行时从代码中设置了一个源代码,并且添加了一个透明/清晰的图像没有任何帮助。您可能想要PreviewMouseUp而不是MouseDown事件
<Image x:Name="imgState" Height="300" Width="300"
PreviewMouseUp="ImgState_OnPreviewMouseUp"
PreviewMouseDown="ImgState_OnPreviewMouseDown"/>
两个选项中的任何一个,您都可以从那里捕获事件。如果上面的答案没有帮助: 这不是一个很好的解决方案,但确实有效很多次: 用一个网格包装你的图像,你将在上面有你的事件
<Grid MouseDown="imgState_MouseDown">
<Image/>
</Grid>
好的,我自己解决了这个问题。 问题在于设置的
OpacityMask=“#00000000”
阻止了图像的出现,因此,如前所述,没有任何可点击的内容。我不知道设置是什么时候设置的,但我认为它是在展开表示法选项卡时自动发生的。
谢谢你帮助我用户控件的代码隐藏中的处理程序imgState\u MouseDown
是什么样子的?请尝试将背景设置为透明。从你发布的xaml来看,鼠标事件没有什么可点击的。请查看我的更新:)请查看我的更新:)你能解释一下PreviewMouseDown和MouseDown之间的区别吗?MSDN表示,当鼠标按钮被按下且位于控件上方时,PreviewMouseDown将启动,而当鼠标按钮被按下时,MouseDown将启动(此事件不应该仅在鼠标位于控件上方时启动吗?)。这个图像在哪里?它的父对象是什么?补充说,父对象是一个网格
图像是否有来源?如果它没有显示任何内容,那么将触发yes no事件。正如我所说,我尝试使用透明背景色、透明300x300图像和白色300x300图像。我应该上传项目,这样你就可以看一看了吗?这是一个VS2013项目
<Grid MouseDown="imgState_MouseDown">
<Image/>
</Grid>