wpf 后面代码的按钮边框厚度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38616005/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:53:24  来源:igfitidea点击:

Button border thickness from code behind

c#wpfformsborderstyling

提问by 91378246

I am very new to wpf, and right now I am working with buttons, and so I want to change buttons border thickness, but from code behind not in XAML, and what I did was next:

我对 wpf 很陌生,现在我正在使用按钮,所以我想更改按钮边框的粗细,但是从后面的代码不在 XAML 中,我接下来要做的是:

var converter = new System.Windows.Media.BrushConverter();
var brush = (Brush)converter.ConvertFromString("#83D744");
btn0.Background = System.Windows.Media.Brushes.Transparent; // This is applied to button
       btn0.BorderThickness = new Thickness(1); //Thickness wont apply to button I dont know why
        btn0.BorderBrush = brush; //This is also applied to button

回答by 91378246

The default border thickness for Buttons is 1 so nothing will change if you set it to 1.

按钮的默认边框粗细为 1,因此如果将其设置为 1,则不会发生任何变化。

To see a change just set it to something different:

要查看更改,只需将其设置为不同的内容:

button.BorderThickness = new Thickness(1, 1, 1, 3);

enter image description here

在此处输入图片说明

回答by Suman Kumar

Since the default button template don't have Border property, More information you can visit: here. So if you want a border around button you have to add your own style, Like:

由于默认按钮模板没有边框属性,更多信息您可以访问:这里。因此,如果您想要按钮周围的边框,则必须添加自己的样式,例如:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="border" BorderBrush="Transparent" BorderThickness="0" Background="Transparent">
               <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

In above code all properties like: BorderBrush, BorderThicknessand Backgroundare hard coded and you can't set these property from code behind. if you want to do so you have to write style like:

在上面的代码中像所有的属性:BorderBrushBorderThicknessBackground被硬编码的,不能设置从后面的代码,这些属性。如果你想这样做,你必须编写如下风格:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
       <Setter.Value>
          <ControlTemplate TargetType="{x:Type Button}">
             <Border x:Name="border" BorderBrush="{TemplateBinding Property=BorderBrush}" BorderThickness="{TemplateBinding Property=BorderThickness}" Background="{TemplateBinding Property=Background}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
             </Border>
          </ControlTemplate>
       </Setter.Value>
    </Setter>
</Style>

And apply this style like:

并应用这种风格,如:

<Grid>
    <Button Name="btnNew" Style="{StaticResource ButtonStyle }" Width="200" Height="50" Click="Button_Click" />
</Grid>

After that you can change those property of Border as your wish, For example:

之后,您可以根据需要更改 Border 的那些属性,例如:

btnNew.Background = Brushes.Black;
btnNew.BorderThickness = new Thickness(4, 5, 7, 9);
btnNew.BorderBrush = Brushes.Red;