Silverlight recursively links Treeview to XDocument

How can I recursively link a Treeview to an XDocument, mapping each XML element to a Node in the Treeview?

The code below should work from my point of view (as well as from the very few posts I've found regarding direct linking), however it doesn't:

<sdk:TreeView ItemsSource="{Binding Path=Elements}" DataContext="{Binding Path=Data}">
  <sdk:TreeView.ItemTemplate>
   <data:HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}">
    <StackPanel Orientation="Vertical">
     <TextBlock Text="{Binding Name}"/>
    </StackPanel>
   </data:HierarchicalDataTemplate>
  </sdk:TreeView.ItemTemplate>
</sdk:Treeview>

      

(Data is a property of type XElement in the DataContext of the parent)

Have I made a mistake somewhere or do I really need to implement an IValueConverter to get the XElement's children?

+2


a source to share


1 answer


The "Elements" element is not an Property, it is a method call.
You cannot bind to method calls in Silverlight.

If you really want this scenario to work, you have 2 options that I can see:
1. Use IValueConverter to retrieve the contents of the Items method.
2. Wrap XDocument in managed classes in the correct hierarchy.



Personally, while option # 1 seems to be the fastest, I believe you will eventually need more time to support and maintain, and then spend an additional 10 minutes building the correct domain model.

Regards,
- Justin Angel

+1


a source







All Articles