Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Grid pixel alignment issues
    primarykey
    data
    text
    <p>I'm using the WPF Grid to align objects, and ran into a rather glaring issue regarding pixel alignment of columns. I tried to remove as many variables as possible, and managed to show the issue in this code:</p> <pre><code>&lt;Window x:Class="Test.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100.5" /&gt; &lt;ColumnDefinition Width="199.5" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Border Background="Red"&gt; &lt;Grid.Column&gt;1&lt;/Grid.Column&gt; &lt;/Border&gt; &lt;Border Background="Red"&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>If you run that sample, it's easy to see that there's an issue in the border between the two columns. It's caused, I believe, because WPF simply alpha-blends one column with the background and the other with the result, even though conceptually both columns are in the same Z, so the pixel should be the blending of the sum of their weights and the background.</p> <p>I understand this is a problematic issue, and naturally I don't intentionally create columns with partial pixel sizes, but the same effect can be easily observed when using star sizes (which I do use a lot).</p> <p>This issue can be worked around by using the SnapsToDevicePixels property (<code>&lt;Grid SnapsToDevicePixels="True"&gt;</code> instead of <code>&lt;Grid&gt;</code>). this works because WPF has internally-consistent rounding, so both columns snap to the same pixel. However, I hit a related problem which is very similar in vein, to which I couldn't find a solution.</p> <p>For some reason, when using the Geometry class, I get the same sort of pixel alignment issues, and I this time I didn't find any sort of work-around. It's as if the pixels are rounded, but now the Geometry is snapped one pixel off, leaving a hole 1-pixel-wide hole.</p> <p>Example code:</p> <pre><code>&lt;Window x:Class="Test.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"&gt; &lt;Grid SnapsToDevicePixels="False"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="100.5" /&gt; &lt;ColumnDefinition Width="199.5" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Border Background="Red"&gt; &lt;Grid.Column&gt;1&lt;/Grid.Column&gt; &lt;/Border&gt; &lt;Border&gt; &lt;Border.Background&gt; &lt;DrawingBrush&gt; &lt;DrawingBrush.Drawing&gt; &lt;GeometryDrawing Brush="Red"&gt; &lt;GeometryDrawing.Geometry&gt; &lt;RectangleGeometry Rect="0,0,1,1"&gt;&lt;/RectangleGeometry&gt; &lt;/GeometryDrawing.Geometry&gt; &lt;/GeometryDrawing&gt; &lt;/DrawingBrush.Drawing&gt; &lt;/DrawingBrush&gt; &lt;/Border.Background&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>Any idea how to get my pixels aligned properly?</p> <p><strong>EDIT:</strong></p> <p>Working correctly now after adding a GuidelineSet according to the answer. Working drawing code is:</p> <pre><code>&lt;DrawingGroup&gt; &lt;DrawingGroup.GuidelineSet&gt; &lt;GuidelineSet GuidelinesX="0,1" GuidelinesY="0,1"&gt;&lt;/GuidelineSet&gt; &lt;/DrawingGroup.GuidelineSet&gt; &lt;GeometryDrawing Brush="Red"&gt; &lt;GeometryDrawing.Geometry&gt; &lt;RectangleGeometry Rect="0,0,1,1"&gt;&lt;/RectangleGeometry&gt; &lt;/GeometryDrawing.Geometry&gt; &lt;/GeometryDrawing&gt; &lt;/DrawingGroup&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload