FocusVisualStyle binding with custom DependencyProperty
<p>I'm trying to change the FocusVisualStyle of a derived button that implement a CornerRadius DependencyProperty. Everything works for the button style, but I can't figure out how to send the CornerRadius value to the FocusVisualStyle.</p>

<p>Here my current code for the FocusVisualStyle:</p>

<pre><code>&lt;Style x:Key="FocusVisualStyle"&gt;
    &lt;Setter Property="Control.Template"&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate&gt;
                &lt;Border BorderBrush="{StaticResource MyFocusBorderBrush}"
                        BorderThickness="1"
                        CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyButton}}}"
                        SnapsToDevicePixels="True"
                        UseLayoutRounding="True"/&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;
</code></pre>

<p>I have also try this form of binding:</p>

<pre><code>CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"
</code></pre>

<p>Any help would be nice :)</p>

<p><strong>EDIT</strong> : As requested, here would be all my code:</p>

<p>MyButton.cs:</p>

<pre><code>public class MyButton : Button
{
    public int CornerRadius
    {
        get { return (int)GetValue(CornerRadiusProperty); }
        set { SetValue(CornerRadiusProperty, value); }
    }

    // DependencyProperty as the backing store for CornerRadius
    public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(
        "CornerRadius",
        typeof(int),
        typeof(MyButton),
        new PropertyMetadata(3)
    );


    static MyButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
    }


}
</code></pre>

<p>Themes\Generic.xaml:</p>

<pre><code>&lt;Style x:Key="FocusVisualStyle"&gt;
    &lt;Setter Property="Control.Template"&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate&gt;
                &lt;Border BorderBrush="Red"
                        BorderThickness="1"
                        CornerRadius="{Binding CornerRadius, ElementName=background}"
                        SnapsToDevicePixels="True"
                        UseLayoutRounding="True" /&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;




&lt;Style TargetType="{x:Type local:MyButton}"&gt;
    &lt;Setter Property="Content" Value="MyButton"/&gt;
    &lt;Setter Property="Background" Value="DarkGray"/&gt;
    &lt;Setter Property="Foreground" Value="White"/&gt;
    &lt;Setter Property="Height" Value="20"/&gt;
    &lt;Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyle}"/&gt;
    &lt;Setter Property="Template"&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType="{x:Type local:MyButton}"&gt;

                &lt;Border x:Name="background"
                        Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource TemplatedParent}}"&gt;
                    &lt;ContentPresenter VerticalAlignment="Center"
                                      HorizontalAlignment="Center" /&gt;
                &lt;/Border&gt;



                &lt;ControlTemplate.Triggers&gt;
                    &lt;Trigger Property="IsMouseOver"
                             Value="True"&gt;
                        &lt;Setter TargetName="background"
                                Property="Background"
                                Value="Gray" /&gt;
                    &lt;/Trigger&gt;

                    &lt;Trigger Property="IsPressed"
                             Value="True"&gt;
                        &lt;Setter TargetName="background"
                                Property="Background"
                                Value="Black" /&gt;
                    &lt;/Trigger&gt;
                &lt;/ControlTemplate.Triggers&gt;

            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;
</code></pre>

<p><strong>EDIT :</strong> Considering I never found a nice solution, here's how I solved it:</p>

<pre><code>public MyButton()
    {
        Loaded += (s, e) =&gt;
        {
            string styleStr = "&lt;Style xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'&gt;" +
            "&lt;Setter Property = \"Control.Template\"&gt; " +
                "&lt;Setter.Value&gt; " +
                    "&lt;ControlTemplate&gt; " +
                        "&lt;Rectangle Margin = \"-2\" " +
                                    "Stroke = \"" + Resource&lt;SolidColorBrush&gt;.GetColor("MaxFocusBorder") + "\" " +
                                    "StrokeThickness = \"1\" " +
                                    "StrokeDashArray = \"1 2\" " +
                                    "RadiusX = \"" + CornerRadius + "\" " +
                                    "RadiusY = \"" + CornerRadius + "\" " +
                                    "SnapsToDevicePixels = \"True\" " +
                                    "UseLayoutRounding = \"True\" /&gt; " +
                    "&lt;/ControlTemplate&gt; " +
               " &lt;/Setter.Value&gt; " +
            "&lt;/Setter&gt; " +
        "&lt;/Style&gt;";

            FocusVisualStyle = (Style)XamlReader.Parse(styleStr);
        };
    }
</code></pre>