按钮上的 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
WPF Dropshadow on Button causes blurry text
提问by Xaphann
This is kind of driving me insane. Adding a DropShadowEffectto a button. In the IDE it looks like this:
这有点让我发疯。DropShadowEffect给按钮添加一个。在 IDE 中,它看起来像这样:


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。如您所见,接下来没有区别。然后我构建项目,当它运行时它看起来像这样:


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 确实让它看起来更好,但它仍然是不对的:


Yet once again in the IDE it looks fine:
再一次在 IDE 中它看起来很好:


回答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 为我呈现带有阴影效果的按钮中清晰的文本。

