Dynamic parameters in XPath WPF bindings.
March 12th, 2009Image via Wikipedia
Since its been a fair while since I posted anything with technical content, I thought I’d lighten the atmosphere with some hardcore XAML extending
I was asked this question a while back, how to parameterize an XPath query in a binding. Well, it is a bit of problem, you know, ranking right up there with global warming, world peace, North Korean space program crisis etc, and like most lazy developer’s (that’s a good thing btw!) I googled first to see what I could turn up.
There were plenty of scams for solving global warming, but not one for Dynamic XPaths in XAML bindings.
Nothing. Zilch. Nada. Zip. There was one post that made me think that it may well be a bit more difficult that I was expecting.
This post by Karl Hulme almost did what I wanted but not quite. it still wasn’t flexible enough to perform the type of binding I wanted to solve this particular problem, which was, to basically externalise rich tool tip data that was held in an external file, so the client could easily update the data to be shown based on control names on a form. The tool tip had to show not only text, but other rich content that was configured from an XML data source.
So after much head scratching, I came up with what I thought was quite a neat solution. Although it doesn’t solve global warming I think it’s quite a neat solution to the XAML problem. It’s a combination of a Value Converter and a Markup Extension, which allows you to specify XPath bound data in a clean way right in the XAML. The tooltip is then templated using these two classes to display the additional content as well as its original content if any.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=05b96833-b15c-4b92-a484-02ed5cd58dbe)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=24cea405-b043-47c9-beb2-73461c036dba)

![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=6c3aab93-b37b-49f6-ad11-5773622c77e0)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=db6b54b9-f504-4644-bbec-58380434630a)