Border.CornerRadiusProperty
is a dependency property of Border, setting it on Button won't do anything.
If you place the edit cursor over your button in your window XAML, go to its property page, click on the little square to the right of "Miscellaneous -> Template" and select "Convert to new resource" then VS will expand out the template as a local resource for you to view and edit. A glance at this template shows that while a border is present in the template, it only supports border brush and thickness:
<Border x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
So if you want to support corners on your buttons you'll need to modify this template and create a property for it to bind to which you can then set in your button declarations. One way to do this is by sub-classing Button
and adding a property such as BorderCornerRadius
there. Another way is to create an attached property, which can then be applied to any framework element, including Button:
public static class ButtonHelper
{
public static CornerRadius GetBorderCornerRadius(DependencyObject obj)
{
return (CornerRadius)obj.GetValue(BorderCornerRadiusProperty);
}
public static void SetBorderCornerRadius(DependencyObject obj, CornerRadius value)
{
obj.SetValue(BorderCornerRadiusProperty, value);
}
public static readonly DependencyProperty BorderCornerRadiusProperty =
DependencyProperty.RegisterAttached("BorderCornerRadius", typeof(CornerRadius), typeof(ButtonHelper), new PropertyMetadata(new CornerRadius(0)));
}
You can then modify the template to use this attached property to set the border corner radius:
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{Binding (local:ButtonHelper.BorderCornerRadius), RelativeSource={RelativeSource TemplatedParent}}"
... etc ...
Now you can use this instead of Border.CornerRadius
. To do it in XAML you simply do this:
<Button x:Name="theButton" Width="100" Height="100"
Template="{DynamicResource ButtonControlTemplate1}"
local:ButtonHelper.BorderCornerRadius="20" />
Or you can do it in code-behind, as in your example code above (don't forget that you still need to override the control template):
Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(ButtonHelper.BorderCornerRadiusProperty, new CornerRadius(20)));
theButton.Style = style;
UPDATE: the answer you linked to works by changing the property for the default Border
style, which you could achieve in code by doing this instead:
Style style = new Style(typeof(Border));
style.Setters.Add(new Setter(Border.CornerRadiusProperty, new CornerRadius(20)));
theButton.Resources.Add(typeof(Border), style);
The problem with this is that it will be applied to all borders in that button, not just the button template but also any in the content or its DataTemplates etc. Sooner or later, as the complexity of your GUI increases, that might come back to haunt you. In general I find that while shortcuts like the above will get the job done in the short term, you'll have far fewer problems down the track by doing things properly.