WPF圆形按钮在Windows 11高对比度主题下变方形的解决问询
解决Windows 11高对比度主题下WPF圆形按钮变方形的问题
问题根源在于Windows 11的高对比度主题会替换WPF控件的默认样式,你之前直接在Button.Resources中设置的Border样式会被系统主题覆盖,导致圆角失效。下面提供两种可靠的解决方法:
方法一:重写Button的ControlTemplate(兼容性最优)
直接自定义Button的控件模板,完全控制内部Border的外观,避免系统主题覆盖圆角设置。同时通过BasedOn继承默认Button的基础属性,保证交互行为不受影响:
<Button Name="captureImageBtn" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="71" Height="70" Click="captureImageBtn_Click" BorderBrush="#e8f4ff" BorderThickness="2"> <Button.Style> <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border CornerRadius="35" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> <StackPanel> <Image Name="imageImg" Source="\Resources\light-icon-camera.png" Height="24"/> <TextBlock Margin="0,5,0,0" Text="{x:Static local:Resources.Toolbar_Image}"/> </StackPanel> </Button>
如果需要保留高对比度下的交互反馈(如鼠标悬停、点击状态),可以在模板中添加触发器:
<ControlTemplate TargetType="Button"> <Border x:Name="border" CornerRadius="35" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="{StaticResource Button.MouseOver.Background}"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="border" Property="Background" Value="{StaticResource Button.Pressed.Background}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
方法二:使用HighContrastAdjustment属性(适用于.NET 5+)
如果你的项目基于.NET 5或更高版本,可以通过设置HighContrastAdjustment="None"阻止系统自动调整控件样式,保留自定义的圆角设置:
<Button Name="captureImageBtn" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="71" Height="70" Click="captureImageBtn_Click" BorderBrush="#e8f4ff" BorderThickness="2" HighContrastAdjustment="None"> <Button.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="35"/> <Setter Property="HighContrastAdjustment" Value="None"/> </Style> </Button.Resources> <StackPanel> <Image Name="imageImg" Source="\Resources\light-icon-camera.png" Height="24"/> <TextBlock Margin="0,5,0,0" Text="{x:Static local:Resources.Toolbar_Image}"/> </StackPanel> </Button>
注意事项
- 方法一兼容性更强,适用于所有.NET版本,推荐优先使用
- 方法二依赖.NET 5+的新特性,若项目基于.NET Framework则无法使用
内容的提问来源于stack exchange,提问作者stefan
相关产品推荐
相关产品推荐

