C# WPF - 点击图像

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

我在 .NET 8 上有一个 WPF 应用程序,其中的窗口加载如下:

var myWindow = new Window
{
    WindowStyle = WindowStyle.None,
    AllowsTransparency = true,
    Background = Brushes.Transparent,
    WindowState = WindowState.Maximized,
    Topmost = true,
    ShowInTaskbar = false
};

myWindow.Show();

此后,

.png
文件将加载到
myWindow

的内容中
myImage = new Image
{
    Source = new BitmapImage(new Uri("./Resources/myImage.png", UriKind.Relative)),
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    Width = 50,
    Height = 50,
    Opactity = 0.5,
    Cursor = System.Windows.Input.Cursors.None,
    IsHitTestVisible = false
};

myWindow.Content = myImage;

我需要做的是点击图像(图像应该被忽略)。

当前图像会阻止图像后面任何图层的所有点击。

IsHitTestVisible = false
这里好像不行...

c# .net wpf .net-8.0
1个回答
0
投票

要允许点击穿过图像并与其后面的图层交互,可以将 Image 的 IsHitTestVisible 属性设置为 false。但是,就您而言,该属性似乎未按预期工作。

var myWindow = new Window
{
    WindowStyle = WindowStyle.None,
    AllowsTransparency = true,
    Background = Brushes.Transparent,
    WindowState = WindowState.Maximized,
    Topmost = true,
    ShowInTaskbar = false,
    IsHitTestVisible = false  // Set IsHitTestVisible to false for the entire window
};

myWindow.Show();

var myImage = new Image
{
    Source = new BitmapImage(new Uri("./Resources/myImage.png", UriKind.Relative)),
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    Width = 50,
    Height = 50,
    Opacity = 0.5,
    Cursor = System.Windows.Input.Cursors.None,
    IsHitTestVisible = true  // Set IsHitTestVisible to true for the image
};

myWindow.Content = myImage;
© www.soinside.com 2019 - 2024. All rights reserved.