Search code examples
.netwpfwindows-7windows-10wpf-style

Why is my WPF style for CornerRadius not being applied in Windows 7?


I created a simple WPF App while answering an SO question, my solution works on my Windows 10 machine, but when I run the app on my Windows 7 laptop the style isn't being applied. I believe this is something to do with Window's style setting over riding my style, how can I force it to use mine instead?

I'm running the same .exe on both machines.

<Window.Resources>
    <Style x:Key="CircleButton" TargetType="Button">
        <Style.Resources>
            <Style TargetType="{x:Type Border}">
                <Setter Property="CornerRadius" Value="1000"/>
            </Style>
        </Style.Resources>
    </Style>
</Window.Resources>

<StackPanel>
    <Button Width="50" Height="50" Margin="10" Style="{StaticResource CircleButton}"/>
    <Button Width="50" Height="50" Margin="10" Style="{StaticResource CircleButton}"/>
    <Button Width="50" Height="50" Margin="10" Style="{StaticResource CircleButton}"/>
</StackPanel>

Windows 10 running .NET 4.8 (App targeting 4.6.2)
Screenshot of three circles produced by the example code in Windows 10 running .NET 4.8 (App targeting 4.6.2)
Windows 7 running .NET 4.7.2 (App targeting 4.6.2)
Screenshot of three circles produced by the example code in Windows 7 running .NET 4.7.2 (App targeting 4.6.2)


Solution

  • The default style on Windows 7 defined in PresentationFramework.Aero.dll uses a ButtonChrome element instead of a Border element, which is why adding your implicit Border style has no effect.

    If you want to apply the Windows 10 style on Windows 7, you could copy the entire ControlTemplate that is defined in PresentationFramework.Aero2.dll on Windows 8 and later and set the Template property in your Style:

    <Style x:Key="CircleButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    ...
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Resources>
            <Style TargetType="{x:Type Border}">
                <Setter Property="CornerRadius" Value="1000"/>
            </Style>
        </Style.Resources>
    </Style>
    

    You can extract the default template by right-clicking on a Button element in design mode in Visual Studio on Windows 10 and choose Edit Template->Edit a Copy.