【问题标题】:Click button through only part of a transparent image in wpf program在wpf程序中仅通过部分透明图像单击按钮
【发布时间】: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

This is what the program looks like when running.

【问题讨论】:

标签: c# wpf button click background-image


【解决方案1】:

我能够在网格中添加一个带有透明填充的矩形。然后我添加了一个事件“MouseDown”交互触发器来通过 DataTrigger 切换可见性,这反过来又改变了背景颜色。

类似于此链接link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 2019-09-21
    • 1970-01-01
    • 2013-08-04
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多