Dan Dan - 1 year ago 188
C# Question

WPF Button ControlTemplate Height binding returns NaN

I'm trying to dynamically set the corner radius of WPF buttons to half of the buttons' height, such that the ends are rounded entirely.

The entry in the Window resources looks like the following, however the

binding doesn't bind, and passes
to the value converter.

I've tried replacing the
with a
to no avail. Snoop shows an error on that
property, being set as
. Am I missing something obvious here?

<Style TargetType="Button">
<Setter Property="Template">
<ControlTemplate TargetType="Button">
<Grid x:Name="grid">
<Border x:Name="border" BorderBrush="#FFFEFEFF" BorderThickness="2" CornerRadius="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Height,Converter={StaticResource HalfConverter}}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>

Answer Source

Instead of binding to the control's Height (which is Double.NaN by default), you should bind to its ActualHeight property:

CornerRadius="{Binding Path=ActualHeight, 
                       RelativeSource={RelativeSource TemplatedParent},
                       Converter={StaticResource HalfConverter}}">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download