摆脱 WPF 中的按钮边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2899948/
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-09-08 21:47:37  来源:igfitidea点击:

Get rid of button border in WPF?

wpfbuttonborder

提问by sanjeev40084

i am trying to get rid of button border and only display text, however a thin line around the text gets displayed even though i set borderThickness to 0 and borderbrush to transparent. alt text

我试图摆脱按钮边框并只显示文本,但是即使我将 borderThickness 设置为 0 并将 borderbrush 设置为透明,也会显示文本周围的细线。 替代文字

my xaml code for save button:

我的保存按钮的 xaml 代码:

<Button Content="save" Name="btnSaveEditedText" 
                Background="Transparent" 
                Foreground="White" 
                FontFamily="Tw Cen MT Condensed" 
                FontSize="30" 
                Margin="-280,0,0,10"
                Width="60"
                BorderBrush="Transparent"
                BorderThickness="0"/>

Is there anyway i can get rid of the button border?

无论如何我可以摆脱按钮边框吗?

回答by bitbonk

You need to override the ControlTemplate of the Button:

您需要覆盖 Button 的 ControlTemplate:

<Button Content="save" Name="btnSaveEditedText" 
                Background="Transparent" 
                Foreground="White" 
                FontFamily="Tw Cen MT Condensed" 
                FontSize="30" 
                Margin="-280,0,0,10"
                Width="60"
                BorderBrush="Transparent"
                BorderThickness="0">
    <Button.Template>
        <ControlTemplate TargetType="Button">
             <ContentPresenter Content="{TemplateBinding Content}"/>
        </ControlTemplate>
    </Button.Template>
</Button>

回答by Dan Asberry

The method that I recently found to be most useful for this was to have your button use the style of a toolbars. This will only use the image or text while only showing button borders on mouse over.

我最近发现对此最有用的方法是让您的按钮使用工具栏的样式。这将仅使用图像或文本,而仅在鼠标悬停时显示按钮边框。

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
        Content="save"
        Name="btnSaveEditedText" 
        Background="Transparent" 
        Foreground="White" 
        FontFamily="Tw Cen MT Condensed" 
        FontSize="30" 
        Margin="-280,0,0,10"
        Width="60"
        BorderBrush="Transparent"
        BorderThickness="0" />

回答by ChrisF

You need to create a new Template for your buttons.

您需要为按钮创建一个新模板。

The easiest way to do this is open your project in Expression Blend, select the button and then right click and select "Edit Template > Edit a Copy..". This copies the existing template into one you can modify. It's easier if you create it in a resource dictionary.

最简单的方法是在 Expression Blend 中打开您的项目,选择按钮,然后右键单击并选择“编辑模板 > 编辑副本...”。这会将现有模板复制到您可以修改的模板中。如果在资源字典中创建它会更容易。

Then select the template and on the Resource tab (on the right of the UI) select the ButtonFocusVisual. Select the Properties tab and expand the Miscellaneous section. This has BorderStyle and BorderThickness fields (among others). Set the style to None.

然后选择模板并在 Resource 选项卡(在 UI 的右侧)上选择 ButtonFocusVisual。选择属性选项卡并展开杂项部分。这有 BorderStyle 和 BorderThickness 字段(等等)。将样式设置为无。

回答by samneric

Templates will not solve this problem, your only course of action is to modify the WPF control. The solution is here:

模板不能解决这个问题,你唯一的做法是修改 WPF 控件。解决方案在这里:

How to remove ButtonChrome border (when defining the template of a border)?

如何删除 ButtonChrome 边框(定义边框模板时)?