为什么我的CornerRadius WPF样式未在Windows 7中应用? [英] Why is my WPF style for CornerRadius not being applied in Windows 7?
问题描述
我创建了一个简单的WPF应用程序,同时回答一个SO问题,我的解决方案在Windows 10机器上可以运行,但是当我在Windows 7笔记本电脑上运行了该应用程序,但没有应用样式.我相信这与Window的样式设置有关,而不是沿用我的样式,我该如何强迫它改用我的样式?
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?
我在两台计算机上都运行相同的.exe.
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>
运行.NET 4.8的Windows 10(面向4.6.2的应用)
运行.NET 4.7.2(应用定位4.6.2)的Windows 7
Windows 10 running .NET 4.8 (App targeting 4.6.2)
Windows 7 running .NET 4.7.2 (App targeting 4.6.2)
推荐答案
PresentationFramework.Aero.dll
中定义的Windows 7上的默认样式使用ButtonChrome
元素而不是Border
元素,这就是为什么添加隐式
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.
如果要在Windows 7上应用Windows 10样式,则可以复制Windows 8及更高版本上在PresentationFramework.Aero2.dll
中定义的整个ControlTemplate
,并在Style
中设置Template
属性:
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>
您可以通过在Windows 10上的Visual Studio中的设计模式下右键单击Button
元素来提取默认模板,然后选择编辑模板"->编辑副本".
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.
这篇关于为什么我的CornerRadius WPF样式未在Windows 7中应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!