【问题标题】:Get rid of button border in WPF?摆脱 WPF 中的按钮边框?
【发布时间】:2011-02-23 09:42:23
【问题描述】:

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

我的保存按钮的 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"/>

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

【问题讨论】:

  • 简写:&lt;Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" &gt;&lt;/Button&gt;,来自similar post

标签: wpf button border


【解决方案1】:

你需要重写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>

【讨论】:

  • 您的建议将删除按钮的所有突出显示/按下样式。
  • 是的。如果您想保留它,则必须获取控制模板的副本(例如,使用表达式混合)对其进行修改(并保留您要保留的部分),然后将其应用于您的按钮。
【解决方案2】:

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

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

【讨论】:

  • 圣桶丹!这是天才!
  • 需要注意的是,如果按钮在工具栏内,它将不起作用。
【解决方案3】:

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

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

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

【讨论】:

【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-03
    • 2018-07-24
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多