鼠标事件未在Windows WPF中触发

问题描述 投票:0回答:3

我有一个简单的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组件下定义的所有事件都没有触发,我尝试了所有googles首页搜索,但没有一个工作。

我读到了我的应用程序中路由事件的区别,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>

但是我不能想象它修复它。

c# wpf events wpf-controls mouseevent
3个回答
0
投票

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


0
投票

适用于高级用户

对于初学者

  • Image组件需要一个源来触发由@Örvar发布的事件。添加透明源是解决方案,虽然是一个hacky。

如果我或其他人找到更好的解决方案,我会更新这个答案。 (或者投票和离开,这也有助于...)


-1
投票

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

WPF Image Control with Click Event

© www.soinside.com 2019 - 2024. All rights reserved.