Saturday, January 17, 2009

IronPython and data binding plus the PyBinding markup extension

XAML is a markup language used to describe user interfaces for the Windows Presentation Foundation (WPF) UI library. PyBinding is a library created by Pixel-in-Gene allowing you to use IronPython for custom expressions in data binding situations: "essentially a DSL for creating Bindings". A second blog entry discusses a detail of the WPF framework that makes this less useful than it could be in some situations.
Many of us have felt the need to have custom expressions inside of Bindings. The need has ranged from applying mathematical operators to calling methods on bound objects. For simple needs you can probably create a custom IValueConverter or a IMultiValueConverter that can do the job, but when you want to use arbitrary expressions, something more elaborate is needed. The ScriptConverter class that I blogged about earlier provides the underpinning for the PyBinding MarkupExtension. The MarkupExtension provides a simplified way of using the ScriptConverter by automatically setting up the Bindings used in the Python statements. Thus the PyBinding extension provides you the following:
  • Allows use of IronPython for scripting arbitrary expressions
  • Each expression will be converted to a Binding or a MultiBinding depending on the number of subexpressions
  • A syntax for specifying the sub-expression. Essentially a DSL for creating Bindings
Rob Oakes has also been thinking about IronPython and data binding. He's written a blog entry with great coverage of the subject. It doesn't show much code, but there is promise of a follow up entry.
In Windows Forms programming, much of the data binding occurs within the Visual Basic or C# code. WPF, however, is substantially different in that much of the syntax required for data binding appears alongside the XAML that defines the user interface. While it is possible to provide the syntax necessary for data binding from either XAML or code, it is easier to do some things from one location rather than the other. As my language of choice when working with WPF is Python, it should be mentioned that it is sometimes necessary to do things from code rather than from XAML. Defining how data should be presented in the XAML while managing the data source from the Python appears to work well.

No comments:

Post a Comment

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