Wednesday, 14 September 2011

Where to Begin?

WPF isn't easy to learn.  The separation of View and Data is a tough concept to get used to if you've been working in any environment where they've been tightly knit for an extended period of time.  However, once you get over the initial learning "hump", it all makes a crazy kinda sense.

It's now been a week or so since I started working with the Windows Presentation Foundation to create modern, professional looking apps rather than have to strong-arm normal Windows WinForm Controls into doing what I want.

It has been an enjoyable - albeit equally frustrating - journey of learning and understanding so far.  The two key points I'm going to dump here that nowhere on the internet really told me until I really dug in deep and that held me up for more than eight hours each are:
  1. Context Menus do not belong to the visual tree of the window.  Why is this important?  When you bind a Menu Item in a Context Menu to a Command, the on-screen Element you're right clicking (or any of its Ancestors for that matter) will not see it.  This is easily rectified by binding the Menu Item's CommandTarget to the parent Context Menu's PlacementTarget.  I wrote a very small SmartMenuItem class that figures out if it is part of a Context Menu and does this binding automatically if it is so that I never have to deal with this problem again.
  2. Using Data Templates for a Style's Content Template is good for presentation only.  When working with Data Templates it's easy to think that you're creating a Template of the Visual Tree that will be copied per instance of the object that uses this Data Template.  This is not the case.  The Data Template represents a single instance of the visual tree that will be shared across all objects implementing that Data Template.  This is where Binding comes in.  By binding sections of the Data Template to the values in the parent you require to be unique; you can make the one Data Template represent different information depending on the context of the instance referencing it.
So at the moment these are my two main revelations regarding WPF.  There is absolutely no doubt in my mind that WPF is an incredibly powerful tool that, once understood, will greatly reduce the development time of any visually impressive Windows application.

No comments:

Post a Comment