wpf wpf中怎么彻底去掉按钮边框?

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

How do you completely remove the button border in wpf?

wpfbuttontransparency

提问by Simon

I'm trying to create a button that has an image in it and no border - just like the Firefox toolbar buttons before you hover over them and see the full button.

我正在尝试创建一个按钮,其中有一个图像并且没有边框 - 就像在您将鼠标悬停在它们上面并查看完整按钮之前的 Firefox 工具栏按钮一样。

I've tried setting the BorderBrushto Transparent, BorderThicknessto 0, and also tried BorderBrush="{x:Null}", but you can still see the outline of the button.

我试过将 设置BorderBrushTransparentBorderThickness0,也试过BorderBrush="{x:Null}",但您仍然可以看到按钮的轮廓。

回答by Simon

Try this

尝试这个

<Button BorderThickness="0"  
    Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >...

回答by Snehal

You may have to change the button template, this will give you a button with no frame what so ever, but also without any press or disabled effect:

您可能需要更改按钮模板,这将为您提供一个没有框架的按钮,但也没有任何按下或禁用效果:

    <Style x:Key="TransparentStyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="Transparent">
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And the button:

和按钮:

<Button Style="{StaticResource TransparentStyle}"/>

回答by Alfred B. Thordarson

What you have to do is something like this:

你必须做的是这样的:

<Button Name="MyFlatImageButton"
        Background="Transparent"
        BorderBrush="Transparent"
        BorderThickness="0" 
        Padding="-4">
   <Image Source="MyImage.png"/>
</Button>

Hope this is what you were looking for.

希望这就是你要找的。

Edit:Sorry, forgot to mention that if you want to see the button-border when you hover over the image, all you have to do is skip the Padding="-4".

编辑:抱歉,忘了提及,如果您想在将鼠标悬停在图像上时看到按钮边框,您所要做的就是跳过Padding="-4"

回答by Nam G VU

I don't know why others haven't pointed out that this question is duplicated with this one with accepted answer.

我不知道为什么其他人没有指出这个问题与这个已接受的答案重复。

I quote here the solution: You need to override the ControlTemplateof the Button:

我在这里引用了解决方案:您需要覆盖ControlTemplateButton

<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 Сергей Гусев

You can use Hyperlink instead of Button, like this:

您可以使用 Hyperlink 而不是 Button,如下所示:

        <TextBlock>
            <Hyperlink TextDecorations="{x:Null}">
            <Image Width="16"
                   Height="16"
                   Margin="3"
                   Source="/YourProjectName;component/Images/close-small.png" />
            </Hyperlink>
        </TextBlock>

回答by micahtan

You may already know that putting your Button inside of a ToolBar gives you this behavior, but if you want something that will work across ALL current themes with any sort of predictability, you'll need to create a new ControlTemplate.

您可能已经知道将 Button 放在 ToolBar 内会给您这种行为,但是如果您想要一些可以在所有当前主题中工作且具有任何类型可预测性的东西,您将需要创建一个新的 ControlTemplate。

Prashant's solution does not work with a Button not in a toolbar when the Button has focus. It also doesn't work 100% with the default theme in XP -- you can still see faint gray borders when your container Background is white.

当 Button 具有焦点时,Prashant 的解决方案不适用于不在工具栏中的 Button。它也不能 100% 使用 XP 中的默认主题——当您的容器背景为白色时,您仍然可以看到微弱的灰色边框。

回答by Ricardo

Programmatically, you can do this:

以编程方式,您可以执行以下操作:

btn.BorderBrush = new SolidColorBrush(Colors.Transparent);

回答by Moumit

Why don't you set both Background & BorderBrushby same brush

你为什么不把两者都设置Background & BorderBrush相同brush

 <Style TargetType="{x:Type Button}" >
        <Setter Property="Background" Value="{StaticResource marginBackGround}"></Setter>
        <Setter Property="BorderBrush" Value="{StaticResource marginBackGround}"></Setter>            
 </Style>

<LinearGradientBrush  x:Key="marginBackGround" EndPoint=".5,1" StartPoint="0.5,0">
    <GradientStop Color="#EE82EE" Offset="0"/>
    <GradientStop Color="#7B30B6" Offset="0.5"/>
    <GradientStop Color="#510088" Offset="0.5"/>
    <GradientStop Color="#76209B" Offset="0.9"/>
    <GradientStop Color="#C750B9" Offset="1"/>
</LinearGradientBrush>