Wednesday 21 September 2011

Things You Won't Know Until You Know

During most of my free time over the past week or so I've been delving even deeper into WPF and building test applications here and there.  I've also run into many seemingly complicated issues only to find out that they boil down to really simple WPF design nuances.  To save anybody else from suffering the same trouble I've experienced I have compiled a list, as the title suggests, of things you won't know until you know (about WPF):

---------------------------------------

The Importance of the Background Property
For a FrameworkElement to be involved in hit-testing (i.e. respond to being clicked, mouseover, etc.) it requires a Background Brush value.  If you don't want your element to have a background colour then feel free to set it's Background property to "Transparent".  Also, don't be fooled if your element has a default colour.  By default the Background brush is set to null and this means no hit-testing.  As a quick aside; it doesn't matter where you set the Background property.  In your window xaml, a style, the code behind, wherever; it will work.

The Extreme Power of Binding
Suffice it to say; binding is - in my opinion - the single most powerful concept that underpins the whole of WPF.  By enabling you to make loose connections not only to properties and objects but also to list of objects you can harness your data with a UI that seems almost self-aware.  I won't be blogging about Binding unless asked to as Microsoft have already done an amazing job but I will be posting little snippets here and there about how Binding helped me get out of a jam.

Canvas Width and Height
Canvas objects don't have Width or Height values unless you have set them by explicitly stating "Width=".  If you need the values of a Canvas' on screen width and height then reference them with ActualWidth and ActualHeight instead to retrieve accurate values.

ContextMenu and the Visual Tree
I've mentioned it already in both of my two previous blog posts but I'll say it here again for emphasis; ContextMenus are on a separate Visual Tree to the rest of the application and this can cause issues.  For a more in depth explanation and a fix please see my previous post: SmartMenuItem - The Power of Binding.

FindResource and generic.xaml
You may not have a generic.xaml file but if you do; the method, FindResource, does not search your generic.xaml file for your resource.  For your resource to be able to be found using that method you will need to define it in a resource dictionary that you reference explicitly from the App, Window, UserControl, etc.  For more information on how to do this see the MSDN article on ResourceDictionary.MergedDictionary.

Microsoft Expression Blend
I've only recently discovered that while Microsoft Expression Blend does cost a fair bit, the SDK doesn't.  This means you can play with all the extremely powerful toys that come with Blend (Behaviors, to name one) directly in your xaml just without the fancy Blend front end.  Download the Microsoft Expression Blend SDK for free from Microsoft's Official Site

PropertyMetaData and Double
When defining DependencyProperties in your classes you may want a dependency property of type double.  This is absolutely fine.  However; make sure that when setting a default value you explicitly pass a double to the PropertyMetaData.  For example, if you want your Dependency Property to have a default of zero and create the PropertyMetaData like this:  new PropertyMetaData(0) then your code will throw an error.  Why? Because you just invoked the PropertyMetaData constructor for an int and this isn't compatible with your double DependencyProperty.  To avoid this problem, use new PropertyMetaData(0D) instead.

---------------------------------------

And that's it for the moment.  No doubt this list will grow as I continue my journey with the Windows Presentation Foundation.  If you've experienced similar plight at the hands of WPF then please feel free to let me know what it was in the comments below and I'll add it to the list.

No comments:

Post a Comment