【问题标题】:wpf create button template with invisible clickable areawpf创建具有不可见可点击区域的按钮模板
【发布时间】:2013-04-02 10:19:02
【问题描述】:

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

这是我的尝试:

<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 方法被调用。 但内部按钮动画未激活。

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

【问题讨论】:

  • 为什么你的按钮包含另一个按钮?
  • @dowhilefor 欢迎您提出其他方式来创建具有不可见可点击边距的按钮
  • 这很正常。因为内部按钮不接收“点击事件”,因为外部按钮接收。此外,外部按钮ControlTemplate 不包含任何点击动画,因此您不会看到。
  • 查看@Snehal 的回答,了解对我来说完美的解决方案。 stackoverflow.com/questions/995757/…

标签: wpf xaml


【解决方案1】:

有多种方法,例如将所有命令和事件路由到您的内部按钮,但这可能意味着背后的代码需要大量工作。 “唯一的 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>

但是,当用于不同的主题时,这可能看起来不太好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2018-01-05
    • 2013-10-15
    • 1970-01-01
    相关资源
    最近更新 更多