【发布时间】: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属性设置才能正常工作?
谢谢
【问题讨论】: