Why is ShowGridLines so slow?

.net, c#, performance, wpf

Solution

The `ShowGridLines` property is not optimised for runtime as it is intended to be a design time tool only.

From the documentation:

Enabling grid lines creates dotted lines around all the elements within a Grid. Only dotted lines are available because this property is intended as a design tool to debug layout problems and is not intended for use in production quality code. If you want lines inside a Grid, style the elements within the Grid to have borders.

Problem

I have noticed that enabling `ShowGridLines` on a `Grid` causes a huge performance hit. I have a `Grid` within a `ScrollViewer`. When I scroll manually, the application runs fine. However, if I scroll programmatically, using a timer to scroll smoothly, it runs very slow with `ShowGridLines` on. With `ShowGridLines` off, the scrolling completes in less than 1/2 a second as expected. With `ShowGridLines` on, the scrolling takes 2-5 seconds and jerks unpredictably. Why would `ShowGridLines` cause such a performance penalty? Why would it not do so when scrolling manually? Is there a way around this?

Original source