Button border thickness from code behind
Solution 1
The default border thickness for Buttons is 1 so nothing will change if you set it to 1.
To see a change just set it to something different:
button.BorderThickness = new Thickness(1, 1, 1, 3);
Solution 2
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
, BorderThickness
and Background
are hard coded and you can't set these property from code behind. if you want to do so you have to write 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="{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:
btnNew.Background = Brushes.Black;
btnNew.BorderThickness = new Thickness(4, 5, 7, 9);
btnNew.BorderBrush = Brushes.Red;
Admin
Updated on June 07, 2022Comments
-
Admin almost 2 years
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:
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
-
Admin almost 8 yearsBut it does not look like button's border thickness is 1, I would say it looks like it's 3 or smth, thats reason why I want to set it up to 1, I will post screenshot later and you will see what I am talking about :)
-
91378246 almost 8 yearsThe default button in WPF has a border property, no need to add it (msdn.microsoft.com/en-us/library/…)
-
Suman Kumar almost 8 yearsDefinitely @91378246 , Button class has a property named
Border
, But I am talking about Template.ContentPresenter
is the thing which is responsible for any content with in button and thatContentPresenter
don't haveBorder
so for changeBorder
thickness first you have to add it and the easiest way is wrapContentPresenter
inside aBorder
and play with it like I did it above. There can be other way but This is what I found easy.