如何显示在WPF中单击(按下)的按钮?
问题描述:
在鼠标上移时,按钮应该显示背景边框
On mouse is up button should show the background border
我已经创建了简单的样式
I have created a simple style
<UserControl.Resources>
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
并在按钮中
<Button Height="20" Width="20" Padding="0,0,0,0" DockPanel.Dock="Top" Grid.Row="0" Grid.Column="1" Click="button_click" Style="{StaticResource TransparentButton}"
BorderBrush="Transparent" BorderThickness="0" Background="Transparent">
<Button.Content>
<Image Source="../Resources/Help_icon.png" Stretch="UniformToFill" />
</Button.Content>
</Button>
但是在这种情况下,当按下按钮时,它不会显示在UI中。用户应该感到按钮被按下。
But in this case when button is pressed it doesn't show up in UI. User should feel that button is pressed.
感谢和问候
答
我不确定您在视觉上想要什么,但是如果您希望在按下按钮时边框改变颜色,您可以像这样修改模板:
I'm not sure what you want visually, but if you want the border to change color when the button is pressed down, you would modify your template like this:
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" Background="Transparent" BorderThickness="1" BorderBrush="Black">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Transparent" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>