WPF用户控件附加到边框时显得模糊

问题描述 投票:1回答:2

我遇到了一个非常奇怪的问题。这是我的观点:

<Grid>
    <Border x:Name="C01" VerticalAlignment="Center" Panel.ZIndex="2" HorizontalAlignment="Center" />
</Grid>

这是我要在视图中显示的用户控件:

<UserControl x:Class="Nwp.UserComponents.ULogin"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         Width="350" Height="220">
    <Grid>
        <Border Margin="10" BorderBrush="DimGray" BorderThickness="1" Background="White">
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0" BlurRadius="10"/>
            </Border.Effect>
            <Grid>
                ... here's content
            </Grid>
        </Border>
    </Grid>
</UserControl>

为了显示用户控件,我将其添加为边框'C01'的子控件,如下所示:

C01.Child = new ULogin();

到目前为止,一切正常,看起来像这样:

“看起来不错”

注意,用户控件的宽度和高度是2个偶数:350和220。如果其中之一更改为奇数,则UserControl会显示有点模糊:

“宽度351”

当我再次使用偶数:330x200时,没有模糊:

“

使用奇数330x201,再次模糊:

“

有人知道如何解决这个问题吗?

c# wpf blur
2个回答
3
投票

您只需要将UIElement.SnapsToDevicePixels property上的UIElement.SnapsToDevicePixels设置为Border

True

从MSDN上的链接页面:

获取或设置一个值,该值确定在渲染期间此元素的渲染是否应使用设备特定的像素设置。

...

对于以高于每英寸96点的速度运行的设备,像素捕捉渲染可以最大程度地减少单位实线附近的抗锯齿视觉伪像。


0
投票

之所以发生此现象,是因为您要将阴影效果应用到边框。在WPF中,此效果将影响此边框内的所有控件。因此控件显得模糊。

要解决此问题,请将阴影效果应用于没有内容的边框。这样,阴影效果将仅应用于边框本身:

    <Border Margin="10" BorderBrush="DimGray" BorderThickness="1" Background="White" 
        SnapsToDevicePixels="True">
        <Border.Effect>
            <DropShadowEffect ShadowDepth="0" BlurRadius="10"/>
        </Border.Effect>
        <Grid>
            ... here's content
        </Grid>
    </Border>
© www.soinside.com 2019 - 2024. All rights reserved.