[英]Mouse events not firing in Windows WPF
I have a simple WPF application 我有一个简单的WPF应用程序
<Grid>
<Border BorderBrush="Black" BorderThickness="1"
HorizontalAlignment="Left" Height="388" Margin="10,10,0,0" VerticalAlignment="Top" Width="329"/>
<Image x:Name="PreviewImg" HorizontalAlignment="Left" Height="400" Margin="10,10,0,0" VerticalAlignment="Top" Width="329" MouseDown="PreviewImgMouse" AllowDrop="True" PreviewMouseWheel="PreviewImg_PreviewMouseWheel" MouseEnter="Entered" Focusable="True"></Image>
<Label Content="Matched Products" HorizontalAlignment="Left" Margin="344,10,0,0" VerticalAlignment="Top" Width="290"/>
<Label Content="Unknown Products" HorizontalAlignment="Left" Margin="661,10,0,0" VerticalAlignment="Top" Width="290"/>
<TextBlock x:Name="MatchedProducts" HorizontalAlignment="Left" Margin="349,36,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="362" Width="295"/>
<TextBlock x:Name="UnknownProducts" HorizontalAlignment="Left" Margin="666,36,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="362" Width="295"/>
<Grid HorizontalAlignment="Left" Height="50" Margin="10,410,0,0" VerticalAlignment="Top" Width="957">
<Button x:Name="FinalizeBtn" Content="Finalize" HorizontalAlignment="Left" Margin="826,10,0,0" VerticalAlignment="Top" Width="120" Height="30"/>
<Button x:Name="NewEntryBtn" Content="Create new entry" HorizontalAlignment="Left" Margin="701,10,0,0"
VerticalAlignment="Top" Width="120" Height="30"/>
<Button x:Name="FreeBtn" Content="Free Button" HorizontalAlignment="Left" Margin="576,10,0,0" VerticalAlignment="Top" Width="120" Height="30"/>
</Grid>
</Grid>
All the events defined under the Image component are not firing, I tried all of googles first page searches, but none of them work. 在Image组件下定义的所有事件均未触发,我尝试了所有google第一页搜索,但没有一个起作用。
I read about the difference of Routed events in my application both MouseDown
and PreviewMouseDown
should work. 我了解了我的应用程序中Routed事件的区别,
MouseDown
和PreviewMouseDown
都应该起作用。
The only thing that can block the image is Border
component, but I did try without it and still nothing, and since it is declared first the image should overlay it...? 唯一可以阻挡图像的是
Border
组件,但我确实尝试了它,但仍然没有任何东西,并且由于首先声明了图像,因此图像应该覆盖它...?
All function names are correct since they were automatically generated. 所有函数名称都是正确的,因为它们是自动生成的。
I can paste in the .cs file if it can help. 如果可以,我可以粘贴.cs文件。
Thanks 谢谢
E1: Somehow it works now, but I have no idea what fixed it... One change I did was mode the image inside the border: E1:它现在可以以某种方式起作用,但是我不知道该怎么修复...我所做的一个更改是在边框内设置图像的模式:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="388" Margin="10,10,0,0" VerticalAlignment="Top" Width="329">
<Image x:Name="PreviewImg" Height="400" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" Width="329" MouseDown="PreviewImgMouse" AllowDrop="True" PreviewMouseWheel="PreviewImg_PreviewMouseWheel" MouseEnter="Entered" Focusable="True"/>
</Border>
But I can't image that this fixed it. 但是我无法想象这可以解决它。
You need to define a source for your image like <Image Source="image.jpg"
. 您需要为图像定义一个源,例如
<Image Source="image.jpg"
。 Without the image the events will not fire. 没有图像,事件将不会触发。
For advanced users 对于高级用户
For beginners 对新手而言
If I or somebody else finds a better solution I will update this answer. 如果我或其他人找到更好的解决方案,我将更新此答案。 (Or down vote and leave, that also helped...)
(或者投下反对票,这也有帮助...)
One way of achieving what you want is to place the image in a button and removing the button styles. 实现您想要的一种方法是将图像放置在按钮中并删除按钮样式。 There is an excellent example here:
这里有一个很好的例子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.