Wpf 按钮模板:没有背景的边框不起作用
Wpf Button Template: Border without background not working
我根据这个 Whosebug 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}"
,按钮单击事件仅在我准确单击按钮文本时才有效。如果我在边框区域内(而不是在文本处)执行单击,则不会触发任何单击事件。这种行为的技术原因是什么?为什么边框需要 Background
属性 设置才能正常工作?
感谢
Border.Background Property
它说:
The Brush that draws the background. This property has no default value.
可能是因为这个原因,如果您将其设为空,则当您单击事件触发的文本时,边框就像空的一样。
Background="Transparent"
会让你的按钮看起来像空的,但是当你点击空按钮时 space 它也会触发一个事件。
<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>
我根据这个 Whosebug 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}"
,按钮单击事件仅在我准确单击按钮文本时才有效。如果我在边框区域内(而不是在文本处)执行单击,则不会触发任何单击事件。这种行为的技术原因是什么?为什么边框需要 Background
属性 设置才能正常工作?
感谢
Border.Background Property 它说:
The Brush that draws the background. This property has no default value.
可能是因为这个原因,如果您将其设为空,则当您单击事件触发的文本时,边框就像空的一样。
Background="Transparent"
会让你的按钮看起来像空的,但是当你点击空按钮时 space 它也会触发一个事件。
<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>