Question

I'm having a bit o' trouble...

Here is my "mappings.xml" file...

<?xml version="1.0" encoding="utf-8"?>
<mappings>
  <mapping QID="info1">
    <empty></empty>
  </mapping>
  <mapping QID="info2">
    <empty></empty>
  </mapping>
</mappings>

My method that loads the XML and sets it as the ItemsSource for the listbox:
(Note: I didn't use <XMLDataProvider> in XAML because it delivers a set of XML.XMLElement, rather than the LINQ-Compatible XElement I want to work with.

Private Property myCollectionView as CollectionView
Private Property mappingsEnum as IEnumerable(Of System.Xml.Linq.XElement)

    Sub LoadXML()
        mappingsEnum = XDocument.Load("mappings.xml").Root.Elements

        'using collection view so I can apply 
        'filtering to the list of <mapping> objects

         myCollectionView = CollectionViewSource.GetDefaultView(mappingsEnum)

        myListBox.ItemsSource = myCollectionView       
End Sub

in XAML

<Grid>
   <ListBox x:Name="myListBox" ScrollViewer.VerticalScrollBarVisibility="Auto"   DockPanel.Dock="Bottom" ScrollViewer.CanContentScroll="True"   VirtualizingStackPanel.VirtualizationMode="Recycling"   VirtualizingStackPanel.IsVirtualizing="True" SelectionMode="Extended">
         <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                              <!--  WHY DOESN'T THIS WORK?? -->    
                              <TextBlock Text="{Binding XPath=@QID}">
                            </Grid>
                        </DataTemplate>
         </ListBox.ItemTemplate>
</ListBox>
</Grid>

I've tried setting the XPath to ".@QID" as well, that made no difference. I inspected the myListBox.Items collection and confirmed the items are in fact XElement objects that look like <mapping QID="..."><empty/></mapping> so I don't think there is an issue there.

The end result is a listBox that fills with empty objects. I can filter them, sort them, and add non-bound text to them... but any binding to the source object is left unrendered.

Was it helpful?

Solution

http://msdn.microsoft.com/en-us/library/system.windows.data.binding.xpath.aspx

Some quote from there:

LINQ to XML objects XDocument and XElement do not use XPath. For details, see How to: Bind to XDocument, XElement, or LINQ for XML Query Results.

And some example how to bind to XElement

http://msdn.microsoft.com/en-us/library/cc165615.aspx

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