Binding XAML to XML with XPATH & Attributes
In the current version of TVScout I’m developing, I’m adding in support for movies (as mentioned earlier) from TheMovieDB. I wanted a selection dialog incase multiple matches were returned went searching for a movie. Using XAML’s XML binding, that’s pretty simple, especially using Blend to auto-generate the DataTemplate for the Listbox.
Most of the data ends up being displayed via XAML like this:
<TextBlock Text="{Binding Mode=OneWay, XPath=title}"/>
TheMovieDB’s API returns posters as an array
<poster size="original">..</poster>
<poster size="thumb">..</poster><poster size="mid">..</poster>
Obviously giving the full-sized poster when asking people to choose between movies is a little much – filesize difference alone would be a couple of hundred kilobytes, multiplied by however many results there are. Using XPath, that’s not a problem, it would just be poster[@size='thumb'] if I wanted the thumbnail sized one.
I had just assumed that I could do an easy modification to the auto-generated binding (which selected the first result, which invariably was the full sized poster), but that wouldn’t compile. Instead I had to use the below syntax:
<Image> <Image.Source> <Binding> <Binding.XPath> poster[@size='thumb'] </Binding.XPath> </Binding> </Image.Source> </Image>
Not exactly as compact or as readable as it should be (I was expecting it to complain about unescaped text, until I realised it was the child content of Binding.XPath), but it works just as well.
