你如何完全删除wpf中的按钮边框?

127 wpf transparency button

我正在尝试创建一个按钮,其中包含一个图像而没有边框 - 就像Firefox工具栏按钮,然后将鼠标悬停在它们上面并查看完整按钮.

我已尝试设置BorderBrushto Transparent,BorderThicknessto 0,并尝试过BorderBrush="{x:Null}",但您仍然可以看到按钮的轮廓.

Sim*_*mon 250

试试这个

<Button BorderThickness="0"  
    Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >...
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,它禁用了将"Horizo​​ntalContentAlignment"设置为"Stretch"的效果. (10认同)
  • 这会将我的按钮变为切换按钮.当我单击按钮时它保持选中状态,直到我单击另一个按钮.我怎么能阻止它这样做呢? (10认同)
  • @Cœur指定WPF的问题不是Silverlight (3认同)
  • 极好的!我发现的所有其他解决方案都非常复杂,涉及覆盖按钮的所有样式。 (2认同)
  • 如果可以的话,我会两次投票.保存了很多代码以获得我想要的外观. (2认同)

Sne*_*hal 49

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

    <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>
Run Code Online (Sandbox Code Playgroud)

按钮:

<Button Style="{StaticResource TransparentStyle}"/>
Run Code Online (Sandbox Code Playgroud)


Alf*_*son 23

你要做的是这样的事情:

<Button Name="MyFlatImageButton"
        Background="Transparent"
        BorderBrush="Transparent"
        BorderThickness="0" 
        Padding="-4">
   <Image Source="MyImage.png"/>
</Button>
Run Code Online (Sandbox Code Playgroud)

希望这是你想要的.

编辑:对不起,忘了提一下,如果你想在鼠标悬停在图像上时看到按钮边框,你所要做的就是跳过Padding =" - 4".


Nam*_* VU 20

我不知道为什么其他人没有指出这个问题与这个问题重复,并且接受了答案.

我在这里引用的解决方案:你需要重写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>
Run Code Online (Sandbox Code Playgroud)

  • 如果您没有在按钮内放置任何内容,它将不会响应点击.您可以通过将ContentPresenter包装在具有透明背景的边框中来解决此问题.这样,您可以制作任意大小的空白/透明按钮放置在图像上. (5认同)

mic*_*tan 2

您可能已经知道将 Button 放在 ToolBar 中会产生这种行为,但是如果您想要能够在所有当前主题中工作并具有任何可预测性的功能,则需要创建一个新的 ControlTemplate。

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