Tips: How to use SnapsToDevicePixels when creating a ControlTemplate

After investigating the default control templates shipped by WPF, I conclude rules of thumb for creating the custom CoontrolTemplate. (The rules don’t necessarily apply on every single default control template of WPF, but I can say they work for most of them.)

1. use Border or Chrome as the outermost level of the custom control template, and set the Border/Chrome’s SnapsToDevicePixels to true

2. if the custom control template is supposed to contain ContentPresenter, ItemsPresenter or ScrollViewer, set the ContentPresenter/ItemsPresenter/ScrollViewer’s SnapsToDevicePixels to {TemplateBinding UIElement.SnapsToDevicePixels}. No matter how many levels stay between the outermost level and the ContentPresenter/ItemsPresenter/ScrollViewer, no need to set the SnapsToDevicePixels for them.

Exceptions:

1. No need to set SnapsToDevicePixels for custom control templates of Window, Frame and their subclasses. But it is also OK to set it.

2. ContentControl’s control template does not have a top level container working as layout, but only has a ContentPresenter. Anyway you do not need to customize a control template for ContentControl either.

3. Rules above don’t by default apply on ScrollViewer, ReaderPageViewer, FlowDocumentPageViewer, FlowDocumentScrollViewer, FlowDocumentReader, DocumentViewer and their subclasses. It is better to check their default control templates before starting to make the custom ones.

P.S.:

What is SnapsToDevicePixels about?

Advertisements

~ by Martin on October 11, 2008.

One Response to “Tips: How to use SnapsToDevicePixels when creating a ControlTemplate”

  1. Thanks a lot, this really helped me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: