在 wpf 中更改按钮边框粗细?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15636160/
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
Change button border thickness in wpf?
提问by Andrei Neculai
Why the border thickness of the Button doesn't change?
为什么Button的边框粗细没有变化?
If I change the border thickness to 1 or 100, it doesn't matters. It's the same. I would like to change it using Style, not Custom Template.
如果我将边框粗细更改为 1 或 100,则无关紧要。一样的。我想使用Style来改变它,而不是Custom Template。
<Window x:Class="GUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="newYellowButton" TargetType="{x:Type Button}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
<Setter Property="Background">
<Setter.Value>
<RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.5,0.5" RadiusX="0.5" RadiusY="0.5" SpreadMethod="Pad" ColorInterpolationMode="SRgbLinearInterpolation">
<GradientStop Color="#FFEEEE3B" Offset="0.5" />
<GradientStop Color="#FFF0E49A" Offset="1" />
</RadialGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="9"/>
<Setter Property="BorderBrush" Value="Blue" />
<Setter Property="Padding" Value="-4"/>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource newYellowButton}" Content="Ok"/>
</Grid>
采纳答案by Hossein Narimani Rad
You can do it by changing the Button's ControlTemplate. Copy those style, brushes and ... to you resource dictionary then change the values you want.
您可以通过更改按钮的 ControlTemplate 来实现。将这些样式、画笔和...复制到您的资源字典中,然后更改您想要的值。
To change the border thickness find the following code and make changes you want:
要更改边框粗细,请找到以下代码并进行所需的更改:
...
<Border
x:Name="Border"
CornerRadius="2"
BorderThickness="1" //CHANGE THIS VALUE
Background="{StaticResource NormalBrush}"
BorderBrush="{StaticResource NormalBorderBrush}">
<ContentPresenter
Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
...

