WPF’s Most Important Property – UseLayoutRounding

Ever since the introduction of WPF, applications developed using the technology have all had a similar look – fuzzy. In .NET 4, the developers at Microsoft made great strides in the clarity and readability of WPF applications.

Up until .NET 4, developers have used many tricks to get icons and line edges clearer than they are by default. Whereas some tricks may still be needed, one new property puts an end to much of the fuzziness frustration – UseLayoutRounding.

Below is an example of how UseLayoutRounding can help us. The left image is how WPF acts by default. The right is with UseLayoutRounding set to true.

UseLayoutRounding Example

Left: UseLayoutRounding Disabled Right: UseLayoutRounding Enabled

The above examples are made using a very basic WPF application. The only difference between the applications is whether or not UseLayoutRounding is enabled.

<Window x:Class="UseLayoutRoundingTutorial.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="MainWindow"
       Height="350"
       Width="525"
       UseLayoutRounding="True">
  <Grid>
    <Image Source="image.png" Width="100" Height="100" />
  </Grid>
</Window>

UseLayoutRounding should be set on a root element, like the main window in this example. MSDN explains it best:

You should set UseLayoutRounding to true on the root element. The layout system adds child coordinates to the parent coordinates; therefore, if the parent coordinates are not on a pixel boundary, the child coordinates are also not on a pixel boundary.

That does it for the UseLayoutRounding property. I can’t imagine any WPF application where this should not be added, as it can only improve the visual quality of your applications.

Leave a Reply

Your email address will not be published.