7

I'm trying to make a control that draws a red cross in its center. I want the cross to be one pixel wide, and I want to disable antialiasing and make it snap to the screen's pixels.

The control works, but if I add it inside a Grid that has a splitter, when I drag the splitter one of the lines will sometimes disappear. If I put it inside a grid with a horizontal splitter, the horizontal line will sometimes disappear and if I put it inside a grid with a vertical splitter, the vertical line will sometimes disappear.

How can I stop the lines from disappearing?

Here is the xaml code:

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

And here is the code for the HalfValueConverter:

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

This is how it looks when you drag the splitter into the right position:

Line gone

And this is how it should look:

Line visible

Ove
  • 6,227
  • 2
  • 39
  • 68

1 Answers1

3

In order to stop the lines from disappearing I needed to also use UseLayoutRounding="True" in addition to SnapsToDevicePixels.

Ove
  • 6,227
  • 2
  • 39
  • 68