【发布时间】:2016-12-15 13:55:00
【问题描述】:
我正在尝试摆脱按钮周围的白色边框。我正在向Button 添加图像。好像不是按钮本身,而是图片的边框。
这是 XAML,
<Style x:Key="MyButton" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0" Background="{TemplateBinding Background}" CornerRadius="5" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.5" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
还有Button 本身,
<Button Name="Button2" Content="stuff" Height="25" Grid.Column="1" Grid.Row="1"
Style="{StaticResource MyButton}" Click="Button2_OnClick">
<Button.Background>
<ImageBrush ImageSource="Images\ButtonEnterLight.png"></ImageBrush>
</Button.Background>
</Button>
这就是结果,
我还需要做什么才能让它消失?上周一直困扰着我。
编辑:这是 Live Visual Tree 的屏幕截图,展开到第一个按钮,其中嵌入了图像。
【问题讨论】:
-
如果投反对票的人愿意分享他们投反对票的原因会很好吗?
-
你用Snoop或者VS检查过边框是从哪里来的吗?
-
@Lennart,原谅我,我不知道 Snoop 是什么,但是我在哪里可以找到这个寄宿生在 VS 中的位置?单击边框只是注册它是按钮的一部分,但在非图像嵌入按钮上,没有使用相同
Style的边框。 -
你有 Visual Studio 2015 吗?它包含 UI 调试工具(此处解释为 msdn.microsoft.com/en-us/library/mt270227.aspx)。您要做的是在 Live Visual Tree 中搜索您的 Button,然后检查该边框的来源。我怀疑这是您在 ControlTemplate 中的边界。你需要那个吗?如果您根本不需要边框,则可以将其删除,并且模板中只有 ContentPresenter。
-
将此添加到您的按钮样式
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>或仅输入 BorderThickness=0