Wednesday, December 10, 2008

IronPython is the new IValueConverter, IMultiValueConverter

A blog entry on using IronPython to create a DSL (Domain Specific Language) to evaluate expressions in WPF XAML (the XML used to describe user interfaces for Windows Presentation Foundation and Silverlight):
"Applying simple mathematical operations inside WPF DataBindings has always been a non-trivial task. By default the {Binding} or MultiBinding syntax does not allow the use of arbitrary expressions inside the Path."

"There are several cases where you would like to do a little bit of math inside of the {Binding} expressions."

"This requires the use of a converter that would do the actual mapping of values to the bounds of the Canvas."

"Ideally you would want the complete flexibility of a scripting language inside of your Binding expressions. Note however that there is a tendency to over-use / abuse a scripting functionality. That however is for the team to decide what level of sanity needs to be maintained in the script. With judicious usage, a scripting language for evaluating binding expressions is supremely powerful. As of today, IronPython is a great choice for exposing scripting functionality."

"ScriptConverter is a custom converter that I implemented that evaluates python expressions and statements. It hosts an IronPython engine and evaluates scripts specified inside the binding expression. Note that it is just a class that implements both IValueConverter and IMultiValueConverter. It is not a MarkupExtension or a custom Binding class, although one could certainly do both. The decision to implement it as a converter rather than a markup-extension was purely to keep the well-known {Binding} and MultiBinding syntax in your Xaml files."

No comments:

Post a Comment

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