dimanche 2 août 2015

Binding to XML doc in XAML with multiple indexes

I am binding to an XML document in an XAML TextBlock statement and here is my XML document:

<Library>
    <Category Name="fiction">
        <Author Name="john Doe"/>
            <Book Title="Book A"/>
            <Book Title="Book B"/>
        <Author Name="Jane Doe"/>
    </Category>
    <Category Name="non-fiction"/>
    <Category Name="reference"/>
</Library>

In my xaml code, I can successfully bind to the Author "John Doe" using the following

Text="{Binding XPath=(/Library/Category/Author)[1]/@Name}" // Returns 'John Doe'.

However, if I try and bind to the first Book title (Book A) by John Doe using any of the following XPath statements, I get nothing.

Text="{Binding XPath=(/Library/Category/Author)[1]/(Book)[1]/@Title}" // Empty

Text="{Binding XPath=((/Library/Category/Author)[1]/Book)[1]/@Title}" //  Empty

Text="{Binding XPath=(/Library/Category/Author)[1]/Book[1]/@Title}" // Empty

Can someone tell me the correct syntax? Ideally, I want to be able to specify the Author by name rather index. Something like:

Text="{Binding XPath=((/Library/Category/Author)[@Name='John Doe']/Book)[1]/@Title}" //  Empty

Aucun commentaire:

Enregistrer un commentaire