WPF: Embedding DLR Scripts in XAML (Python, Ruby) - Part 2, A Simple DLR Markup Extension

A Visual Studio 2008 Solution with the complete code listing for this series is attached to the final part.

This article demonstrates how to embed DLR scripts in XAML using a custom MarkupExtension. The scripting language can be any language supported by the DLR, such as Python or Ruby.

The first article in this series demonstrated how to host the DLR and presented some utility methods that make executing scripts simple. Here we build upon this to create a simple MarkupExtension that evaluates a DLR expression.

WPF: XAML MarkupExtension as a Macro and Object Factory

A Visual Studio 2008 Solution with the complete code listing for this posting is attached.

The MarkupExtension in XAML opens up a world of opportunity to make XAML work the way you want it to. It reminds me somewhat of macros where we think of expanding the macro as substituting the macro for something else. If we consider that the MarkupExtension does nothing more than provide a value that is substituted for the MarkupExtension, it is very much like expanding a Macro.

Parameterised macros are extremely powerful as they do more than just save you typing, they can form a domain specific language. MarkupExtensions are stateful and you can set their state from XAML, leading to the idea that the substitution can be parameterised.

In this article I will demonstrate how to use a custom MarkupExtension as a macro or an object factory. The ideas will be applied to to a simple macro expansion case and the more pertinent case of creating a custom ValueConverter in a manner that is simpler than that provided by raw XAML.

Subscribe to RSS - MarkupExtension