繁体   English   中英

Windows WPF中未触发鼠标事件

[英]Mouse events not firing in Windows WPF

我有一个简单的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>

在Image组件下定义的所有事件均未触发,我尝试了所有google第一页搜索,但没有一个起作用。

我了解了我的应用程序中Routed事件的区别, MouseDownPreviewMouseDown都应该起作用。

唯一可以阻挡图像的是Border组件,但我确实尝试了它,但仍然没有任何东西,并且由于首先声明了图像,因此图像应该覆盖它...?

所有函数名称都是正确的,因为它们是自动生成的。

如果可以,我可以粘贴.cs文件。

谢谢

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>

但是我无法想象这可以解决它。

您需要为图像定义一个源,例如<Image Source="image.jpg" 没有图像,事件将不会触发。

对于高级用户

对新手而言

  • Image组件需要一个源来触发@Örvar发布的事件。 解决方案尽管添加了黑手,但添加透明的源是解决方案。

如果我或其他人找到更好的解决方案,我将更新此答案。 (或者投下反对票,这也有帮助...)

实现您想要的一种方法是将图像放置在按钮中并删除按钮样式。 这里有一个很好的例子:

具有Click事件的WPF图像控件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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