按钮上的 WPF 阴影会导致文本模糊

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21538296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:42:48  来源:igfitidea点击:

WPF Dropshadow on Button causes blurry text

c#wpfdropshadow

提问by Xaphann

This is kind of driving me insane. Adding a DropShadowEffectto a button. In the IDE it looks like this:

这有点让我发疯。DropShadowEffect给按钮添加一个。在 IDE 中,它看起来像这样:

enter image description here

在此处输入图片说明

Second button is for reference with no DropShadowEffect. As you can see there next no difference. Then I build the project and when it runs it looks like this:

第二个按钮供参考,没有DropShadowEffect。如您所见,接下来没有区别。然后我构建项目,当它运行时它看起来像这样:

enter image description here

在此处输入图片说明

What is causing this change? Here is the XAML for the two buttons:

是什么导致了这种变化?这是两个按钮的 XAML:

<Button Name="clearButton" Content="Clear" Click="clearButton_Click" Margin="5,0,5,0" MaxWidth="80" MinHeight="40" 
    TextOptions.TextFormattingMode="Display">
<Button.Effect>
    <DropShadowEffect BlurRadius="5" ShadowDepth="3" />
</Button.Effect>
</Button>
<Button Content="Clear" Margin="5,5,5,0" MaxWidth="80" MinHeight="40"  TextOptions.TextFormattingMode="Display" />

Edit: Taking @gretro does make it look better but it still is not right:

编辑:使用@gretro 确实让它看起来更好,但它仍然是不对的:

enter image description here

在此处输入图片说明

Yet once again in the IDE it looks fine:

再一次在 IDE 中它看起来很好:

enter image description here

在此处输入图片说明

回答by Mike Strobel

Your entire button is rendering on a cross-pixel boundary. Note how the 1-point border actually spans multiple pixels, causing a blurring effect.

您的整个按钮都在跨像素边界上呈现。请注意 1 点边框实际上如何跨越多个像素,从而导致模糊效果。

Try setting UseLayoutRounding="True"on a parent or ancestor element. The further up the tree, the better (your view's root visual would be ideal). You can also try SnapsToDevicePixels="True".

尝试UseLayoutRounding="True"在父元素或祖先元素上设置。树越往上越好(您的视图的根视觉将是理想的)。你也可以试试SnapsToDevicePixels="True"

回答by gretro

Remove the attached Property TextOptions.TextFormattingMode="Display". This is what is causing the button to be blurry.

删除附加的 Property TextOptions.TextFormattingMode="Display"。这就是导致按钮模糊的原因。

<Button Grid.Row="25" Grid.Column="0" Content="Clear">
    <Button.Effect>
       <DropShadowEffect BlurRadius="5" ShadowDepth="3" />
    </Button.Effect>
</Button>

This XAML renders crystal clear text in the button with the shadow effect for me.

此 XAML 为我呈现带有阴影效果的按钮中清晰的文本。