title: How to Bind an XmlDataProvider Class Property to a XAML TreeView
body: <p>I would like to bind a TreeView control I have defined in XAML to a property in its code-behind class. I already read through a <a href="http://blogs.msdn.com/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx" rel="nofollow noreferrer">WPF Basic Data Binding FAQ</a>, but the example in the comments at the very bottom of the page didn't work when I tried to use an XmlDataProvider as the binding source.</p>
  
  <p>How can I modify the following code so that the binding is defined in the XAML, rather than in the class's constructor? In other words, how can I modify the TreeView's <code>ItemsSource</code> attribute to reference a property in its code-behind class?</p>
  
  <h3>SomeClass.xaml - Works</h3>
  
  <pre><code>&lt;UserControl x:Class="SomeNamespace.SomeClass"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;UserControl.Resources&gt;
          &lt;XmlDataProvider x:Key="SomeTreeData" /&gt;
      &lt;/UserControl.Resources&gt;
      &lt;TreeView Name="SomeTree" ItemsSource="{Binding Source={StaticResource SomeTreeData}, XPath=*}"&gt;
          &lt;TreeView.Resources&gt;
              &lt;HierarchicalDataTemplate DataType="items" ItemsSource="{Binding XPath=*}"&gt;
                  &lt;TextBlock Text="{Binding XPath=@Header}" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
              &lt;HierarchicalDataTemplate DataType="item" ItemsSource="{Binding XPath=*}"&gt;
                  &lt;TextBlock Text="{Binding XPath=@Header}" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/TreeView.Resources&gt;
      &lt;/TreeView&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <h3>SomeClass.xaml.cs - Works</h3>
  
  <pre><code>public partial class SomeClass : UserControl
  {
      public SomeClass()
      {
          InitializeComponent();
  
          XmlDataProvider lSomeTreeData
              = this.FindResource("SomeTreeData") as XmlDataProvider;
          lSomeTreeData.Document = new XmlDocument();
          lSomeTreeData.Document.LoadXml("&lt;items xmlns=\"\" Header=\"Some items\"&gt;&lt;item Header=\"Some item\" /&gt;&lt;/items&gt;");
      }
  }
  </code></pre>
  
  <h3>SomeClass.xaml - Desired</h3>
  
  <p>Note the <code>{SOME MAGIC}</code> in the TreeView's <code>ItemsSource</code> attribute.</p>
  
  <pre><code>&lt;UserControl x:Class="SomeNamespace.SomeClass"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;TreeView Name="SomeTree" ItemsSource="{Binding Source={SOME MAGIC}, XPath=*}"&gt;
          &lt;TreeView.Resources&gt;
              &lt;HierarchicalDataTemplate DataType="items" ItemsSource="{Binding XPath=*}"&gt;
                  &lt;TextBlock Text="{Binding XPath=@Header}" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
              &lt;HierarchicalDataTemplate DataType="item" ItemsSource="{Binding XPath=*}"&gt;
                  &lt;TextBlock Text="{Binding XPath=@Header}" /&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/TreeView.Resources&gt;
      &lt;/TreeView&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <h3>SomeClass.xaml.cs - Desired</h3>
  
  <pre><code>public partial class SomeClass : UserControl
  {
      public XmlDataProvider SomeXmlDataProvider { get; set; }
  
      public SomeClass()
      {
          InitializeComponent();
  
          this.SomeXmlDataProvider = new XmlDataProvider();
          this.SomeXmlDataProvider.Document = new XmlDocument();
          this.SomeXmlDataProvider.Document.LoadXml("&lt;items xmlns=\"\" Header=\"Some items\"&gt;&lt;item Header=\"Some item\" /&gt;&lt;/items&gt;");
      }
  }
  </code></pre>