Change button border thickness in wpf?

青春壹個敷衍的年華 提交于 2019-12-02 01:28:54

问题


Why the border thickness of the Button doesn't change?

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.

 <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>


回答1:


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.

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>
...


来源:https://stackoverflow.com/questions/15636160/change-button-border-thickness-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!