如何移除WPF中ImageBrush的模糊投影效果?
解决WPF按钮使用ImageBrush背景出现模糊投影的问题
问题根源是MaterialDesign主题的Button默认带有投影效果或特定背景渲染逻辑,和自定义ImageBrush叠加后产生了异常模糊。以下是几种可行的解决方法:
方法1:完全重写按钮模板,移除默认视觉效果
直接自定义按钮的ControlTemplate,替换掉MaterialDesign自带的带投影的模板:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style TargetType="Button" x:Key="SeamlessBtn"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Padding" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Transparent"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
方法2:直接禁用MaterialDesign的投影效果
引入MaterialDesign命名空间后,通过ShadowAssist关闭按钮投影:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"> <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button" x:Key="SeamlessBtn"> <Setter Property="materialDesign:ShadowAssist.ShadowDepth" Value="None"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> </Trigger> </Style.Triggers> </Style> </ResourceDictionary>
方法3:改用Image作为按钮内容(更推荐)
放弃用ImageBrush做背景,直接将Image放在按钮的Content区域,避免背景层叠加的渲染冲突:
按钮代码:
<Button Style="{StaticResource SeamlessBtn}" Width="30" Height="30" Click="ToggleTheme" BorderBrush="Transparent" Margin="10,10,105,408"> <Image Source="/Images/BootstrapIcons-MoonFill.png" Width="24" Height="24"/> </Button>
配套样式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button" x:Key="SeamlessBtn"> <Setter Property="Padding" Value="0"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> </Trigger> </Style.Triggers> </Style> </ResourceDictionary>
内容的提问来源于stack exchange,提问作者probablydiary
相关产品推荐
相关产品推荐

