I’m working on a little project at the moment which is dynamically building a WPF user interface based on a configuration file loaded when the application starts. This involves code to manually create all of the bits and pieces that are normally specified directly in a Xaml file.

One useful shortcut I’ve found is to leverage .NETs built in TypeConverter framework - the same technique used by the Xaml loader itself.

For example, when creating a GridLength as part of setting up a Grid, the following shortcut works well:

var definition = // string definition for row height
var converter 
    = TypeDescriptor.GetConverter(typeof(GridLength));
var length
    = (GridLength)converter.ConvertFromString(definition);

The TypeDescriptor system provides access to a heap of useful capabilities that we’ve only just touched upon here. Worth following up.

Comments

blog comments powered by Disqus