3

我在将 XmlDataProvider 绑定到 WPF TreeView 时遇到了一些问题。

TreeView 是 XAML 定义的,如下所示:

<TreeView ItemsSource="{Binding Path=SelectedSystemVersionHistory}"/>

我在 XML 文件中的节点的 TreeView 的父网格资源中有一个 HierarchicalDataTemplate:

<HierarchicalDataTemplate DataType="Documentation" ItemsSource="{Binding XPath=*}">
  <TextBlock Text="{Binding XPath=@SoftwarePackage}"/>
</HierarchiclaDataTemplate>

我的 ViewModel 有这个属性:

public XmlDataProvider SelectedSystemVersionHistory
{
  get
  {
    String file = GetHistoryFile(); //returns full Filepath
    return new XmlDataProvider()
    {
      source = new Uri(file, UriKind.Absolute),
      XPath= "History"
    };
  }
}

Xml 看起来像这样:

<?xml version="1.0" standalone="yes" encoding="utf-8">
  <History>
    <Documentation SoftwarePackage="SoftwarePackageName">
      <Entry>...</Entry>
    </Documentation>
  </History>

问题是 TreeView 没有显示任何内容,所以出了什么问题?我已经为此工作了好几天...... :o( 谢谢你的帮助。

4

1 回答 1

1

不幸的是,您不能直接绑定 XmlDataProvider 的 Document 和 Source 属性,它们不是 DependencyProperties。另请参阅如何将 XmlDataProvider.Source 绑定到 MVVM 属性

您可以做的是将 Treeview 的 DataContext 分配给 XMLDataProvider:

<TreeView DataContext="{Binding SelectedSystemVersionHistory}" ItemsSource="{Binding XPath=Documentation}"/>
于 2011-12-07T10:41:22.937 回答