我在绑定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没有显示任何内容,那么出了什么问题呢?我已经研究了好几天了.谢谢你的帮助。
发布于 2011-12-07 18:41:23
遗憾的是,您不能直接绑定XmlDataProvider的Document和Source属性,它们不是DependencyProperties。另请参阅How to bind XmlDataProvider.Source to MVVM property
您可以做的是将树形视图的DataContext分配给XMLDataProvider:
<TreeView DataContext="{Binding SelectedSystemVersionHistory}" ItemsSource="{Binding XPath=Documentation}"/>https://stackoverflow.com/questions/8413148
复制相似问题