gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:24:26 25 4
gpt4 key购买 nike

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

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

这是我想在 View 中显示的用户控件:

    <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();

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

Looks good

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

Width 351

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

330x200

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

330x201

有人知道怎么解决吗?

最佳答案

你只需要设置 UIElement.SnapsToDevicePixels propertyBorderTrue 上:

    <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>

来自 MSDN 上的链接页面:

Gets or sets a value that determines whether rendering for this element should use device-specific pixel settings during rendering.

...

For devices operating at greater than 96 dots per inch (dpi), pixel snap rendering can minimize anti-aliasing visual artifacts in the vicinity of single-unit solid lines.

关于c# - WPF 用户控件附加到边框时显得模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31028924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com