【发布时间】:2017-01-05 14:59:15
【问题描述】:
我正在尝试实现仅单击部分透明背景的功能。背景图像为纯灰色,有 3 个圆形、一个矩形和一个椭圆形。图像是透明的。按钮 1 出现在矩形内,按钮 2 出现在椭圆形内。当程序运行时,两个按钮都可见但当前不可点击,因为图像在网格中并添加在按钮之后。我只想让矩形单击,以便可以单击按钮 1,但仍将图像保持在按钮 2 之前,因此不可单击。
我已经搜索了相当多的解决方案,但没有找到我正在寻找的东西。这个例子是我目前正在研究的一个简化版本 - 但功能本质上是相同的。任何帮助,将不胜感激。
Xaml:
<Window x:Class="TransparentClickThrough.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="ButtonOne" Click="ButtonOne_Click" Content="Button1" Width="50" Height="25" ></Button>
<Button x:Name="ButtonTwo" Content="Button2" Width="50" Height="25" Margin="379,100,59,148" Click="ButtonTwo_Click"></Button>
<Image Source="/TransparentClickThrough;component/background.png" />
</Grid>
</Window>
后面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TransparentClickThrough
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonOne_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("You clicked button 1!");
}
private void ButtonTwo_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("You clicked button 2!");
}
}
}
background - 3 circles, rectangle, and oval are all set to transparent
【问题讨论】:
标签: c# wpf button click background-image