2017-03-03 52 views
-2

我有一個iPhone圖像,我想在主頁按鈕上放置一個點擊事件。點擊事件到圖像的一部分WPF

我該如何用WPF來完成這項工作?我試過在圖片上放置一個按鈕,但是我沒有得到想要的結果。

你對圖像使用什麼類型的控件?

<Window.Background> 
    <ImageBrush ImageSource="Images/Skin/full.png" Stretch="Fill"> 
    </ImageBrush> 
</Window.Background> 
+0

你對圖像使用什麼類型的控件? – FiN

+0

@FiN: <圖像刷的ImageSource = 「圖像/護膚/ full.png」 拉伸= 「填充」> John

+0

WPF有什麼用iPhone? –

回答

1

您可以將圖像中的合適的面板等CanvasGrid並使用不可見按鈕作爲覆蓋。

在資源:

<Style x:Key="InvisibleButtonStyle" TargetType="Button"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <Border Background="Transparent" BorderBrush="Red" BorderThickness="1"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

用法:

<Grid Width="150" Height="150"> 
    <ImageBrush ImageSource="Images/Skin/full.png" Stretch="Fill"/> 
    <Button x:Name="TopLeft" Margin="0,0,100,100" Click="TopLeft_Click" Style="{StaticResource InvisibleButtonStyle}"/> 
    <Button x:Name="TopRight" HorizontalAlignment="Right" Click="TopRight_Click" Margin="0,0,0,100" Width="100" Style="{StaticResource InvisibleButtonStyle}"/> 
    <Button x:Name="BottomLeft" VerticalAlignment="Bottom" Click="BottomLeft_Click" Margin="0,0,100,0" Height="100" Style="{StaticResource InvisibleButtonStyle}"/> 
    <Button x:Name="BottomRight" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="BottomRight_Click" Margin="0,0,0,0" Width="100" Height="100" Style="{StaticResource InvisibleButtonStyle}"/> 
</Grid> 

注意我爲了突出可點擊區域增加了一個紅色邊框的透明按鈕,在現實中,它只會是透明背景。

+0

現在我的問題是我的用戶控件的內容現在因爲MainWindow上的畫布而隱藏起來。 你對此有何建議? – John

+0

@約翰,因爲你不提供你的作文(窗口和用戶控件)的問題,我當然不知道什麼是錯的。 – grek40

0

由於需要圖像的一部分被點擊,我會建議this解決方案。

C /從鏈路P ...

<Canvas> 
    <Image Source="background.png"/> 
    <Ellipse Canvas.Left="82" Canvas.Top="88" Width="442" Height="216" Fill="Transparent" Cursor="Hand" MouseDown="Ellipse_MouseDown_1"/> 
    <Ellipse Canvas.Left="305" Canvas.Top="309" Width="100" Height="50" Fill="Transparent" Cursor="Hand" MouseDown="Ellipse_MouseDown_2"/> 
</Canvas> 
+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [發表評論](/ review/low-quality-posts/15403659) – Tom

+0

@thebluefox編輯... – FiN

0

我已經通過執行以下操作來實現這一點:

<Canvas Height="30" VerticalAlignment="Top" Margin="0,394,0,0" Width="220"> 
      <Button Content="" Canvas.Left="102" Canvas.Top="6" Background="Transparent" Width="16" BorderBrush="Transparent" Cursor="Hand" > 
       <Button.Style> 
        <Style TargetType="{x:Type Button}"> 
         <Setter Property="Background" Value="Transparent"/> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type Button}"> 
            <Border Background="{TemplateBinding Background}"> 
             <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
            </Border> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
         <Style.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter Property="Background" Value="Transparent"/> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </Button.Style> 
      </Button> 
</Canvas> 

只要有設置頁邊距和Canvas左側和頂部位置重疊的點擊區域。