右侧模糊的 WPF 列表框边框
WPF Listbox border on right side blurry
我有一个 WPF 应用程序,其中的列表框显示了一些数据。当我的列表框位于 window 的中心并且 window 宽度不均匀时,右侧的列表框是模糊的。 (绿色为矩形,蓝色部分为文本框)
我几乎在所有地方都尝试应用 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>
在 Window
上设置 UseLayoutRounding="True"
。
我还建议设置 TextOptions.TextFormattingMode="Display"
以提高文本清晰度。
我有一个 WPF 应用程序,其中的列表框显示了一些数据。当我的列表框位于 window 的中心并且 window 宽度不均匀时,右侧的列表框是模糊的。 (绿色为矩形,蓝色部分为文本框)
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>
在 Window
上设置 UseLayoutRounding="True"
。
我还建议设置 TextOptions.TextFormattingMode="Display"
以提高文本清晰度。