我正在尝试创建一个包含图像且没有边框的按钮 - 就像 Firefox 工具栏按钮在您将鼠标悬停在它们上方并查看完整按钮之前一样。
我尝试将BorderBrush
to Transparent
、BorderThickness
to设置为0
,也尝试过BorderBrush="{x:Null}"
,但您仍然可以看到按钮的轮廓。
我正在尝试创建一个包含图像且没有边框的按钮 - 就像 Firefox 工具栏按钮在您将鼠标悬停在它们上方并查看完整按钮之前一样。
我尝试将BorderBrush
to Transparent
、BorderThickness
to设置为0
,也尝试过BorderBrush="{x:Null}"
,但您仍然可以看到按钮的轮廓。
尝试这个
<Button BorderThickness="0"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" >...
您可能必须更改按钮模板,这将为您提供一个没有框架的按钮,但也没有任何按下或禁用效果:
<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>
和按钮:
<Button Style="{StaticResource TransparentStyle}"/>
你必须做的是这样的:
<Button Name="MyFlatImageButton"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="0"
Padding="-4">
<Image Source="MyImage.png"/>
</Button>
希望这就是你要找的。
编辑:对不起,忘了提到,如果您想在将鼠标悬停在图像上时看到按钮边框,您所要做的就是跳过Padding="-4"。
我不知道为什么其他人没有指出这个问题与这个具有接受答案的问题重复。
我在这里引用解决方案:您需要覆盖以下ControlTemplate
内容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>
您可以使用超链接而不是按钮,如下所示:
<TextBlock>
<Hyperlink TextDecorations="{x:Null}">
<Image Width="16"
Height="16"
Margin="3"
Source="/YourProjectName;component/Images/close-small.png" />
</Hyperlink>
</TextBlock>
您可能已经知道将 Button 放在 ToolBar 中会给您带来这种行为,但是如果您想要在所有当前主题中工作且具有任何可预测性的东西,您需要创建一个新的 ControlTemplate。
当按钮具有焦点时,Prashant 的解决方案不适用于不在工具栏中的按钮。它也不能 100% 使用 XP 中的默认主题——当您的容器背景为白色时,您仍然可以看到微弱的灰色边框。
以编程方式,您可以这样做:
btn.BorderBrush = new SolidColorBrush(Colors.Transparent);
你为什么不同时设置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>