wpf创建具有不可见可点击区域的按钮模板

时间:2013-04-10 14:05:51

标签: wpf xaml

我想创建一个按钮模板,使我的按钮周围有一个不可见的可点击区域。 当我按下该区域时,按钮点击事件应为

这是我的尝试:

<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方法。 但是内部按钮动画没有被激活。

当我在边框区域内按下时,我希望内部按钮的行为就像点击一样。

1 个答案:

答案 0 :(得分: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>

但是当用于不同的主题时,这可以看出来。