Can anyone suggest a guideline on when to use SnapsToDevicePixels in WPF 4.0?
Should it only be used occasionally if there is an issue, liberally throughout an App, only on certain controls or what?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Good answers by Spencer and Martin as to the when to align your pixels.
As to the how: I would also point out that one should in WPF 4.0 try using the property
UseLayoutRoundinginstead ofSnapsToDevicePixels.UseLayoutRoundingmakes what you are doing compatible with Silverlight (SnapsToDevicePixelsis not available in Silverlight) … and Microsoft is also encouraging the use ofUseLayoutRoundingoverSnapsToDevicePixelsin its documentation.What is the difference between the two? Well, one big difference is that
UseLayoutRoundingoccurs during the layout phase whileSnapsToDevicePixelsoccurs during the render phase. This makes me speculate thatUseLayoutRoundingis probably a more performant way to go (I haven’t confirmed this, though).All that being said, there will still be reasons to use
SnapsToDevicePixels. In fact, the MSDN documentation points to one. I will add another: it is only withSnapsToDevicePixelsthat you can use guidelines for precise control.Here are some resources on this matter (i.e. pixel snapping and clarity with images, text, and visuals):
Heh. I know my answer was a little more than what you were asking for … but this concept (i.e. resolution independence and the resulting problems it brings and how to get over them) can often be a point of frustration when working with WPF. At the very least, I wanted to point you to the new WPF 4.0 property,
UseLayoutRounding.UPDATE
I just have to add since I’ve seen this over and over … sometimes
SnapsToDevicePixelsworks whenUseLayoutRoundingdoesn’t. I wish I could put a finger on why this is the case, but definitely try UseLayoutRounding first and if that doesn’t work, don’t hesitate to trySnapsToDevicePixels.That line is so sharp it can cut you!