Friday, May 01, 2009

Using IronPython for Converters in Silverlight

Another blog entry from David Seruyange on using IronPython with Silverlight, and another example of using IronPython for binding dynamic types in WPF.
The concept is a really nice one but I’m not wild about having to write a class that implements the IValueConverter interface every time I need some ad-hoc tweaking of the values I get from being data bound and how I’d like to use them within XAML. What set me off this track was a case where I simply needed to invert a boolean – what should just be “not myvalue” ended up requiring a class, an interface, and so on.

Dynamic languages excel at this sort of thing and since they support the ability to evaluate code on the fly I thought it would make sense to write a single implementation of IValueConverter and host Python inside of it to evaluate binding expressions that are passed in.

No magic, just instantiate the Python runtime and then proceed to leverage it in the Convert phase by taking a string as the parameter for the conversion method. Use value as a keyword in your Python expression to get at the value of the underlying data. Once this is added as a static resource, you can leverage it in many ways.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.