9 things I wish I had known about WPF last week
After spending the past week working with WPF for a project for the first time, I’ve come up with a list of things that I wish I had known about before getting into it. This is hindsight, as no one actually knows they need to know these things until it comes up. If I could magically (or even without magic) go back in time, and learn some things about WPF- these would be them. I’d then have 20+ hours of free time to spend bashing my head over some other problem.
Many of these would be more useful with an example. I’ll post examples later, when I have some time to extract them into smaller, more meaningful code blocks.
Microsoft Expressions Blend 3 is your friend. It’s obtuse, but so are most of your other friends.
UIElements that have the DropShadowEffect applied cannot have a transparent background. Doing so will cause all child elements to also acquire the DropShadowEffect. This is likely a bug, because it’s annoying. You can either set the background color, or add some Shape with a background color that Fills the border. It just can’t be empty.
Templated ScrollViewer scrollbars must have the following x:Name, PART_VerticalScrollBar and PART_HorizontalScrollBar. They will not work correctly if not. There are likely other magical control names as well, /sigh.
Event Triggers are out of scope when they are deeply embedded (not the top level) inside of a Style or Template. They should be broken out into their own Style/Template at this point. The project will still compile, but your code-behind for the event will not fire since they are out of scope.
You can save the Strokes collected from an InkCanvas control straight into a GIF by using the old Microsoft.Ink.dll library. A WPF compatible version can be found in the Tablet PC SDK v1.7. I know that you can save it this way via the InkCanvas itself, but if you’re design requires some block of code outside of the scope of the InkCanvas control, this is a way to do it. See Ink.Load() and Ink.Save().
You can assign a NULL style to a control with Style=”{x:Null}”. This will explicitly tell the control to use it’s default Style. This is useful for when you’ve created a global style, but want to exclude specific elements from using it.
Understand the differences between the various layout elements (StackPanel, DockPanel, Grid, Canvas). They are all very different in how elements within them work, and you’ll often find that the reason why XYZ isn’t working the way you think is because you’re using the wrong kind of layout container. Example: UIElements that use a ScrollBar won’t scroll inside of layout containers that don’t specify a Height (like a StackPanel), or automatically Fill (DockPanel).
You can find the default Styles to simple versions of the out-of-the-box controls provided by Microsoft by using Expressions Blend 3. (I’m sure you can do this without Blend as well, though.) Having these Styles is invaluable when trying to figure out how to create your own. Nothing teaches better than a functional example.
Any idea you have that doesn’t require a new property can be Styled or Templated to an existing control. You can do pretty much anything you can imagine with Templates and Styles… Seriously. Only understand that just because you have the power, doesn’t mean that you should use the power.







