views:

27

answers:

1

I want a TreeView with checkboxes and I'm trying to follow this tutorial. However, in it, he creates all his content for the TreeView at runtime. I have an XML file that I have accessible as an XmlDataProvider in my XAML:

<XmlDataProvider Source="XmlData/Versions.xml" XPath="//*[count(*)=0]"
                 x:Key="versionsXml" />

I have a view model class with IsChecked and Name properties, and I want to use this to represent nodes in my XML:

<Versions>
  <Version name="1.0">
    <Version name="1.0.001" />
    <Version name="1.0.002" />
  </Version>
</Versions>

My TreeView will display leaf nodes (i.e., 1.0.001 and 1.0.002) with a checkbox. How can I populate my TreeView with not the XmlDataProvider's content directly, but rather a List<MyViewModel>? I can create a property in my DataContext that returns a List<MyViewModel>, then bind my TreeView to that property, but I don't know how, in a C# property getter, to read in the XML data from the XmlDataProvider. When I use TryFindResource and cast the object result to XmlDataProvider, the Document and Data properties are null for my versionsXml resource (defined in <UserControl.Resources>).

A: 

After reading this thread, I did the following:

<XmlDataProvider Source="XmlData/Versions.xml" XPath="//*[count(*)=0]"
                 x:Key="versionsXml" IsInitialLoadEnabled="True"
                 IsAsynchronous="False" />

Then, in C# to access the XmlDataProvider data since the Data property was no longer null:

var versions = new List<MyViewModel>();
var dataProvider = TryFindResource("versionsXml") as XmlDataProvider;
if (null == dataProvider)
{
    return versions;
}
var nodes = dataProvider.Data as IEnumerable;
if (null == nodes)
{
    return versions;
}
foreach (XmlElement node in nodes)
{
   ...
}
Sarah Vessels