Welcome to WindowsClient.net | Sign in | Join

Nidonocu - typeof(Tea)

Random discoveries as I play with WPF

Sponsors





  • advertise here

SizeToContent border bug

This isn't part three of my theme series as I promised, but instead a random discovery which is after all what I subtitled this blog with. :) I'll get back to that in due course!

Instead, while building a dialog box today, I came across a very weird bug which seemed to have apparent cause. Can you see it?

Border Bug

Those with keen eyes will spot a distracting single pixel width black border along the inside edge of the window on the right and bottom sides (click the image to see it a bit clearer at the original size). This only seemed to kick in once I enabled the SizeToContent property and set it to WidthAndHeight.

SizeToContent is something very useful and not something I wanted to disable, it would let my window resize according to the content inside it which means if the user opens that expander, the window would grow to account for the extra space needed. All very WPF.

None of the child controls were causing it either, as you can see, I had not yet started to style the dialog yet.

After some poking around, I pinned down an option that seemed to be a workaround solution, enabling the SnapsToDevicePixels property on the Window:

In Expression Blend

Or in XAML:

<Window x:Class="Nox.Amuse.Dialogs.QuickConnectSetupDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Quick Connection" ResizeMode="NoResize"
    WindowStartupLocation="CenterOwner" x:Name="DialogWindow"
    SizeToContent="WidthAndHeight"
    ShowInTaskbar="False" SnapsToDevicePixels="True">

As far as I can work out, the bug was caused by my window sizing down to my content, but not quite enough (half a pixel or so, which renders as the greyish single pixel-width lines). SnapToDevicePixels does what it says on the tin, it forces the window to clip down to whole pixels. Feel free to comment if you I've not got this explanation quite right!

After applying the property and running again, I just have the regular border again:

All Gone!

Hope this helps others who get caught by this one!

Comments

Doug Day said:

Actually, it doesn't "Clip" anything.  WPF works in arbitrary units, not pixels, so when displaying your controls on-screen, it has to "cast-down" to pixels to display the object.

In many cases, this can cause a shearing effect, when the object can't figure out which pixel it wants to draw in.  In your case, your window border resided "in-between" pixels, and the WPF system decided to draw on BOTH pixels in order to display properly.

SnapsToDevicePixels forces the render mechanisms in WPF to avoid this behavior, and to choose one-or-the-other when rendering objects.  In many cases, this can clean up the look quite a bit.

Thanks,

-Doug

# April 15, 2008 12:25 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter