我有一个带有显示一些数据的列表框的WPF应用程序。当我在窗口中心放置列表框并且窗口宽度不均匀时,右侧的列表框模糊。 (绿色是矩形,蓝色是文本框)
c# - WPF列表框边框右侧模糊-LMLPHP
我试图在几乎所有地方都应用SnapsToDevicePixels却没有结果。 (我知道它应该由子元素继承,但是我几乎要拔掉头发了)

我可以通过将其设置为HorizontalAlignment="Left"并始终保持固定大小来克服此问题,但是我知道我只是缺少了一些东西,因为文本框确实呈现正确。

这是我的代码(尽可能干净),显示了行为:

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="200"
    Width="401px"
    Background="Red"
    SnapsToDevicePixels="True">
<Grid>
    <Rectangle Width="200" Height="50"  Fill="Blue" VerticalAlignment="Top" />
    <ListBox Height="94" VerticalAlignment="Bottom"  Width="200px" >
        <ListBoxItem>1</ListBoxItem>
        <ListBox.Template>
            <ControlTemplate TargetType="ListBox">
                <ScrollViewer Margin="0" Padding="0" SnapsToDevicePixels="True">
                    <StackPanel Margin="0" IsItemsHost="True" Width="200" Background="GreenYellow"/>
                </ScrollViewer>
            </ControlTemplate>
        </ListBox.Template>
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Width" Value="200"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Grid Background="Green" Height="40" SnapsToDevicePixels="True">
                                <ContentPresenter SnapsToDevicePixels="True"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>
</Window>

最佳答案

UseLayoutRounding="True"上设置Window

我还建议设置TextOptions.TextFormattingMode="Display"以提高文本清晰度。

10-08 07:50
查看更多