C# WPF一个像素宽的线有时会消失
我试着做一个控件,在它的中心画一个红十字。我希望十字是一个像素宽,我想禁用抗锯齿,使其捕捉到屏幕的像素 该控件可以工作,但如果我将其添加到具有拆分器的网格中,当我拖动拆分器时,其中一条线有时会消失。如果我把它放在带有水平拆分器的网格中,水平线有时会消失,如果我把它放在带有垂直拆分器的网格中,垂直线有时会消失 我怎样才能阻止台词消失 以下是xaml代码:C# WPF一个像素宽的线有时会消失,c#,wpf,C#,Wpf,我试着做一个控件,在它的中心画一个红十字。我希望十字是一个像素宽,我想禁用抗锯齿,使其捕捉到屏幕的像素 该控件可以工作,但如果我将其添加到具有拆分器的网格中,当我拖动拆分器时,其中一条线有时会消失。如果我把它放在带有水平拆分器的网格中,水平线有时会消失,如果我把它放在带有垂直拆分器的网格中,垂直线有时会消失 我怎样才能阻止台词消失 以下是xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schemas.mi
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<local:HalfValueConverter x:Key="halfConv" />
<Style TargetType="Line">
<Setter Property="Stroke" Value="Red"/>
<Setter Property="StrokeThickness" Value="1"/>
<Setter Property="RenderOptions.EdgeMode" Value="Aliased"/>
<Setter Property="SnapsToDevicePixels" Value="True" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="2" Background="Black" Name="grdParent">
<Line X1="{Binding ActualWidth, ElementName=grdParent, Converter={StaticResource halfConv}}"
Y1="0"
X2="{Binding ActualWidth, ElementName=grdParent, Converter={StaticResource halfConv}}"
Y2="{Binding ActualHeight, RelativeSource={x:Static RelativeSource.Self}}"
Height="100"
/>
<Line X1="0"
Y1="{Binding ActualHeight, ElementName=grdParent, Converter={StaticResource halfConv}}"
X2="{Binding ActualWidth, RelativeSource={x:Static RelativeSource.Self}}"
Y2="{Binding ActualHeight, ElementName=grdParent, Converter={StaticResource halfConv}}"
Width="100"
/>
</Grid>
<GridSplitter Grid.Row="1" Height="5" HorizontalAlignment="Stretch" Background="Gray" ResizeBehavior="PreviousAndNext" ResizeDirection="Rows" />
</Grid>
</Window>
这是将拆分器拖动到正确位置时的外观:
它应该是这样的:
为了防止线条消失,除了
SnapsToDevicePixels
之外,我还需要使用UseLayoutRounding=“True”
。除了SnapsToDevicePixels
之外,您是否尝试过UseLayoutRounding=“True”
?这很有效,谢谢。如果你写这封信作为回答,我会欣然接受。这是什么意思?你知道为什么吗?我看到视图框缩小时会丢失线条。我无法使用设置UseLayoutRounding=“True”
来显示它。
using System;
using System.Windows.Data;
namespace WpfTest
{
public class HalfValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((double)value / 2);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((double)value * 2);
}
}
}