WPF - Canvas_MouseLeftButtonDown 事件

发布于 2024-10-06 04:48:49 字数 1412 浏览 2 评论 0原文

您好,

我昨天开始使用 WPF 进行开发,但遇到了一些问题。我了解到 Canvas 相当于 WinForms 中的 Panel。然而,我在“点击”事件方面遇到了一些困难。 MouseLeftButtonDown 事件。如果相关或不相关,此图像和画布位于 UserControl

Image + 3 Canvasses

上面的图像基本上是我的我有困难。您看到的 3 张图像是一张图像。你看到的方块都是不同的画布。根据单击的画布,我希望发生不同的事情。

目前我有以下代码:

<Grid>
    <Canvas Name="canvasTerran"  Height="27" Width="26" Margin="88,106,134,106" MouseLeftButtonDown="canvasTerran_MouseLeftButtonDown" />
    <Canvas Name="canvasZerg" Height="27" Width="26" Margin="117,107,105,107" MouseLeftButtonDown="canvasZerg_MouseLeftButtonDown" />
    <Canvas Name="canvasProtoss" Height="27" Margin="145,107,88,107" MouseLeftButtonDown="canvasProtoss_MouseLeftButtonDown" />
    <Image Name="imageRaces" Height="27" Width="73" Stretch="Fill" Source="pack://application:,,,/Images/Races/Races.png" />
</Grid>

当我运行应用程序时(当然,用户控件位于主窗口中)并且我单击画布的位置应该不会发生任何事情。我试图触发的事件:

 private void canvasTerran_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("lolterran", "lol");

            // image on main window
            // .Source = new BitmapImage(new Uri("pack://application:,,,/" + Constants.RACESPATH + "T.png"));
        }

我真的不明白我在这里做错了什么,所以欢迎任何建议。正如我所说,我是 WPF 的新手,所以如果您认为有比我目前正在尝试的更好的方法,请务必说出来!

提前致谢。

Greetings

I started developing with WPF yesterday and have ran into some issues. I came to the understanding that a Canvas was the equivalent to a Panel in WinForms. However I'm running into some difficulties with the 'click' event. The MouseLeftButtonDown event. If it's relevant or not, this Image and the Canvasses are in a UserControl

Image + 3 Canvasses

The above image is basically what I am having difficulties with. The 3 images you see is one image. The squares you see are each different canvases. Depending on what canvas is clicked i want something different to happen.

Currently i have the following code:

<Grid>
    <Canvas Name="canvasTerran"  Height="27" Width="26" Margin="88,106,134,106" MouseLeftButtonDown="canvasTerran_MouseLeftButtonDown" />
    <Canvas Name="canvasZerg" Height="27" Width="26" Margin="117,107,105,107" MouseLeftButtonDown="canvasZerg_MouseLeftButtonDown" />
    <Canvas Name="canvasProtoss" Height="27" Margin="145,107,88,107" MouseLeftButtonDown="canvasProtoss_MouseLeftButtonDown" />
    <Image Name="imageRaces" Height="27" Width="73" Stretch="Fill" Source="pack://application:,,,/Images/Races/Races.png" />
</Grid>

When I run the application (the user control is in the main window ofcourse) and I click where the canvasses should be nothing happens. The event I'm trying to fire:

 private void canvasTerran_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("lolterran", "lol");

            // image on main window
            // .Source = new BitmapImage(new Uri("pack://application:,,,/" + Constants.RACESPATH + "T.png"));
        }

I don't really see what I'm doing wrong here so any suggestions would be welcome. As I said I'm new to WPF so if you think there is a better way than I'm currently trying please do say so!

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人迟暮 2024-10-13 04:48:49

实际问题是画布是透明的。因此,所有事件都不会在画布上停止,而是为其父元素生成。

例如,如果您为画布设置Background=White或Red,则它应该获得MouseLeftButtonDown。

The actual problem is that the canvases are transparent. Thus, all the events do not halt at canvas, but they are generated for its parent element.

If you set Background=White or Red for example for a canvas, it should get MouseLeftButtonDown.

清风无影 2024-10-13 04:48:49

您的图像隐藏了画布,因此在 XAML 中声明图像后声明画布,因此在 UI 中它们将位于画布前面,并且单击将生效。

提示:为画布指定背景颜色只是为了查看它们在 UI 中的呈现方式。当你这样做时,你可以看到图像隐藏了它们。

Your Image hides the canvases so in the XAML declare the canvases after you declare the image, so in the UI they will be in front of it and the the Click will take effect.

Tip: Give the canvas Background colors just to see how they render themselves in the UI. When you do that u can see that the image hides them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文