2016-08-29 64 views
0

我正在嘗試實現僅點擊透明背景部分的功能。背景圖像是純灰色,有3個圓圈,一個矩形和一個橢圓形狀。圖像是透明的。按鈕1出現在矩形內,按鈕2出現在橢圓內。程序運行時,兩個按鈕都可見,但由於圖像位於網格中並且添加在按鈕之後,因此當前不可點擊。我想只做矩形點擊,因此按鈕1可以被點擊,但仍然保持圖像在按鈕2之前,因此它不可點擊。單擊wpf程序中透明圖像的一部分按鈕

我已經搜索了相當多的解決方案,但還沒有找到我在找什麼。這個例子是我目前正在研究的一個虛擬版本 - 但功能基本相同。任何幫助,將不勝感激。

的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.

+0

[在WPF不規則PNG按鈕的單擊事件(可能的重複http://stackoverflow.com/questions/3592404/irregular-png-buttons-click-event-in-wpf ) –

回答

0

我能夠在網格中添加一個帶有透明填充的矩形。然後我添加了一個事件「MouseDown」Interaction觸發器,通過DataTrigger切換可見性,然後更改背景顏色。

這個鏈接類似link