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.


blog comments powered by Disqus
Next Post
Spoken Numbers  15 Apr 2010
Prior Post
Things to install on a new machine  10 Apr 2010
Related Posts
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
April 2010