This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Monday, May 25, 2009

Design Time Data in WPF

I came across an excellent article from Karl Shifflett that discussed a couple of different approaches for viewing data during a design time story.  There is nothing, by default, in Visual Studio that stubs out data for us so that we can see something in our template as we edit it.

image
The design-time and run-time experience, left-to-right.
 

Basically, if you’re creating templates for grids, listboxes or any other control in XAML you’re going to want to be able to see how that data looks without having to compile and run the app continuously.

By (heavily) using Karl’s project as a starting point, I have rebuilt his sample to a WPF-specific approach with imagesome suggestions on how to structure some of the project in a way that can grow a little better.

I also added a concrete example of how a design-time and run-time object could be set up for a little more clarity on how these objects play together.  These are found in the Data hierarchy in the project.

Also, as per Karl’s suggestion, I added a couple of data triggers to play with formatting in that way. There are triggers that reveal changes in either design time or run time (look for the comments).

Again, I borrowed heavily from Karl’s sample here, but wanted to make this sample a little more accessible to anyone who is a little newer to WPF.  There are still tons of opportunities to expand and explore this concept and to figure out how to make it work for you in your application development.

This is all cool, but it’s basically meaning more work and prep on any project you do.  I would love to see an extension to the designer that, in an intelligent way, would allow us to set a boolean value in the DataContext called, say, “EnableDesignTimeData”, that would then stub out as many instances of that class as we specify, perhaps in a “DesignTimeDataRowCount” property on the DataContext.  Allow the designer/support classes to build up rows based on this idea, but also allow us to drop an XML file (or build one through yet another extension to the designer, perhaps with pre-populated rows?) to use as the design-time data.  This would allow us to create data to cover all of the possible formatting scenarios that we may wish to express.

You can download the zip of the project here (VS.Net 2008).

When I get a chance this week, I also hope to MVVM-alize this sample and add a couple of commands.  That would be rockin-cool.

No comments:

Post a Comment