Question

I have a database stored procedure call that, among other columns, returns a column that contains data in XML format. I'm trying to display the results of this call in a grid (the call is made through Entity Framework, so the actual objects bound to the grid are POCO's, and the column in question is a string property).

While the normal columns display correctly, I need to be able to take the XML data in that one column and bind its contents (it will contain multiple nodes) to an ItemsControl within the template for the cell.

For example, let's say I have a grid that displays a collection of the following object:

class Photo
{
    string PhotoId { get; set; }
    string Name { get; set; }
    string TagListXml { get; set; }
}

This is intended to represent a photo, and the TagListXml property contains an XML string listing all of the tags that have been applied to the photo. Something akin to...

<PhotoTags>
    <Tag>Faces</Tag>
    <Tag>People</Tag>
    <Tag>Sepia</Tag>
</PhotoTags>

(While obviously a normal POCO would have a List<string> or something like that, let's just assume for the moment that I must use an XML string)

In my grid, I want to be able to specify an ItemsControl that uses this XML and, ultimately, gives me items Faces, People, and Sepia.

I've tried this for a cell template:

<DataTemplate>
    <ItemsControl ItemsSource="{Binding TagListXml, 
                                Converter={StaticResource xmlConverter}}" />
</DataTemplate>

Where xmlConverter is defined as such:

<dc:StringToXmlConverter x:Key="xmlConverter" XPath="PhotoTags" />

And dc:StringToXmlConverter is a custom IValueConverter that just takes a string value, instantiates an XmlDocument and loads the string, then returns an XmlDataProvider with that new document and the XPath specified above.

While this does not produce any errors either in the form of an exception or a binding error in the Output window, it doesn't do anything (there are no results displayed).

I believe this is because an XmlDataProvider cannot be set to the ItemsSource directly, but rather must be set as the Source of a Binding. (in other words, you must do ItemSource="{Binding Source={StaticResource xmlProvider}}" rather than ItemsSource="{StaticResource xmlProvider}").

I can't seem to get anywhere with it, and I've been banging my head on this for the last couple of hours.

How can I bind an XML string to the ItemsSource of an ItemsControl?

Was it helpful?

Solution

Why not return an XmlNode[] instead of a XmlDataProvider (which is mainly for XAML anyway)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top