Get rid of button border in WPF?

2019-01-11 10:48发布

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 http://i45.tinypic.com/scywye.png

my xaml code for save button:

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

4条回答
混吃等死
2楼-- · 2019-01-11 11:01

You need to override the ControlTemplate of the Button:

<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>
查看更多
家丑人穷心不美
3楼-- · 2019-01-11 11:06

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" />
查看更多
SAY GOODBYE
4楼-- · 2019-01-11 11:07

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.

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.

查看更多
来,给爷笑一个
5楼-- · 2019-01-11 11:13

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

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

查看更多
登录 后发表回答