2013-04-10 136 views
1

我想創建一個按鈕模板,使我的按鈕在其周圍具有不可點擊的可點擊區域。 當我按下區域中的按鈕單擊事件應該是wpf使用隱形可點擊區域創建按鈕模板

這裏是我的嘗試:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="214,150,0,0" Name="button1" VerticalAlignment="Top" Click="button1_Click"> 
      <Button.Template> 
       <ControlTemplate TargetType="Button"> 
        <Border Background="Pink" Padding="25"> 
         <Button Content="{TemplateBinding Content}"></Button> 
        </Border>  
       </ControlTemplate>    
      </Button.Template> 
     </Button> 
    </Grid> 
</Window> 

當我按下邊框內的button1_Click方法被調用。 但內部按鈕動畫未被激活。

我希望內部按鈕的行爲就像點擊邊界區域時一樣。

+0

爲什麼你的按鈕包含另一個按鈕? – dowhilefor 2013-04-10 14:09:35

+0

@dowhilefor歡迎您採取其他方式來創建一個按鈕與隱形可點擊保證金 – Nahum 2013-04-10 14:11:28

+1

這是正常的。因爲內部按鈕沒有收到'點擊事件',因爲外部按鈕的確如此。此外,外部按鈕'ControlTemplate'不包含任何點擊動畫,所以你不會看到一個。 – DHN 2013-04-10 14:12:10

回答

3

有多種方式,如將所有命令和事件路由到內部按鈕,但這可能意味着代碼背後的許多工作。 「僅XAML」解決方案是用的東西複製整個Button Template,並覆蓋像這樣

<ControlTemplate TargetType="Button"> 
    <Border Background="Transparent"> 
     <Border 
      x:Name="Border" 
      Margin="24" 
      CornerRadius="2" 
      BorderThickness="1" 
      Background="{StaticResource NormalBrush}" 
      BorderBrush="{StaticResource NormalBorderBrush}"> 
      <ContentPresenter 
       Margin="2" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       RecognizesAccessKey="True"/> 
     </Border> 
    </Border> 
</ControlTemplate> 

但這可以看看關,不同的主題時。