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

自古美人都是妖i 提交于 2020-02-06 08:45:11

问题


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)

Windows 7 running .NET 4.7.2 (App targeting 4.6.2)


回答1:


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.



来源:https://stackoverflow.com/questions/58452118/why-is-my-wpf-style-for-cornerradius-not-being-applied-in-windows-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!