【问题标题】:Wpf Button Template: Border without background not working [duplicate]Wpf按钮模板:没有背景的边框不起作用[重复]
【发布时间】:2022-01-27 16:28:46
【问题描述】:

我根据这个stackoverflow answer创建了一个圆角半径的按钮,效果很好。

这是 XAML:

<Window x:Class="BorderButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BorderButton"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Height" Value="150"/>
            <Setter Property="Width" Value="150"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="5" 
                                BorderBrush="Black" 
                                Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"></ContentPresenter>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
        <Button Click="Button_Click">Button 1</Button>
    </StackPanel>
</Window>


问题:如果我删除Background="{TemplateBinding Background}",则按钮单击事件仅在我准确单击按钮文本时才有效。如果我在边框区域(而不是文本)内单击,则不会触发单击事件。这种行为的技术原因是什么?为什么border需要Background属性设置才能正常工作?

谢谢

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    Border.Background Property 它说:

    绘制背景的画笔。该属性没有默认值。

    可能正因为如此,如果您将其设为空,则当您单击事件触发的文本时,边框的行为就像是空的。

    Background="Transparent" 将使您的按钮看起来像空的,但是当您单击空白区域时,它也会触发一个事件。

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Height" Value="150"/>
            <Setter Property="Width" Value="150"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border CornerRadius="5" 
                                BorderBrush="Black" 
                                Background="Transparent">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5"></ContentPresenter>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    

    【讨论】:

      猜你喜欢
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2022-01-18
      • 2021-08-16
      • 1970-01-01
      • 2014-11-09
      相关资源
      最近更新 更多