C# 如何在 WPF 中显示单击(按下)按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10839902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:29:33  来源:igfitidea点击:

How can I show a button is clicked(pressed) in WPF?

c#wpf

提问by User123

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>

and in button

并在按钮

<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>

But in this case when button is pressed it doesn't show up in UI. User should feel that button is pressed.

但在这种情况下,当按钮被按下时,它不会显示在 UI 中。用户应该感觉到按钮被按下。

Thanks and regards

感谢致敬

采纳答案by Trevor Elliott

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>

回答by Kevin DiTraglia

When you recreate the ControlTemplate of the button you lose all the default features of the windows button feel. You would need to recreate them with triggers, or not use your own control template.

当您重新创建按钮的 ControlTemplate 时,您将失去 Windows 按钮感觉的所有默认功能。您需要使用触发器重新创建它们,或者不使用您自己的控件模板。

<ControlTemplate.Triggers>
   <Trigger Property="IsPressed" Value="True">
       <Setter ....behavior you want
   </Trigger>
</ControlTemplate.Triggers>

Here's a link to MSDN default control template a button has, you can use it as a reference to recreate some of the behavior you have lost by defining your own.

这是按钮具有的 MSDN 默认控件模板的链接,您可以将其用作参考,以重新创建通过定义自己的行为而丢失的一些行为。

http://msdn.microsoft.com/en-us/library/ms753328%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms753328%28v=vs.85%29.aspx

回答by Nandha kumar

Try this one...

<Style x:Key="PressedButtonEffect" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="border" Background="Transparent" BorderThickness="1" BorderBrush="Gray">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsPressed" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
                            <Setter TargetName="border" Property="BorderThickness" Value="2" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

回答by Robin Bennett

There's a good article herethat explains what's going on and how to fix it. Basically MS did include this feature in the standard button, but the colors they picked for 'mouse down' and 'pressed' are so close that you don't notice the difference.

有一个很好的文章在这里解释这是怎么回事,如何解决它。基本上,MS 确实在标准按钮中包含了此功能,但是他们为“鼠标按下”和“按下”选择的颜色非常接近,您不会注意到其中的区别。

Right-click on the button in the designer, and pick "Edit Template / Edit a Copy..." and then change the colors until you can see a difference. Simple!

右键单击设计器中的按钮,然后选择“编辑模板/编辑副本...”,然后更改颜色,直到您可以看到不同之处。简单的!