Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将xml绑定到wpf treeview?_C#_Xml_Wpf_Mvvm_Treeview - Fatal编程技术网

C# 如何将xml绑定到wpf treeview?

C# 如何将xml绑定到wpf treeview?,c#,xml,wpf,mvvm,treeview,C#,Xml,Wpf,Mvvm,Treeview,如何将xml绑定到wpf treeview?我使用的是Prism mvvm模式。我更喜欢用IList保存循环数据 我已经试过了 但是什么都没用。我的方法是创建一个方法,将树状视图构建到treeview属性。将WPF treeview项绑定设置为类中treeview属性的items属性。当然,在ViewModelBase中实现INotifyPropertyChanged是必不可少的 我很乐意举个例子,但我目前还不能在电脑上上网 我确实在帖子中看到了,并且同意这不是最熟练的方法。但是,由于您已经在使

如何将xml绑定到wpf treeview?我使用的是Prism mvvm模式。我更喜欢用IList保存循环数据

我已经试过了
但是什么都没用。

我的方法是创建一个方法,将树状视图构建到treeview属性。将WPF treeview项绑定设置为类中treeview属性的items属性。当然,在ViewModelBase中实现INotifyPropertyChanged是必不可少的

我很乐意举个例子,但我目前还不能在电脑上上网

我确实在帖子中看到了,并且同意这不是最熟练的方法。但是,由于您已经在使用xml序列化,xml的解析已经完成,现在您只需要使用数据

我认为,如果您不打算序列化,那么您发布的链接将在您试图实现的方法中保持更高的有效性。但这只是IMO。明天有机会时,我将用一些工作代码进行更新,直接将数据绑定到xml的想法听起来很有趣

同时,请查看此链接。它看起来很遥远


好的。这个问题现在已经很老了,但我认为有一种简单的方法可以将XML绑定到树视图。也许这对某人有帮助。 XAML:


就这样。XML文件的内容将显示为树视图。如果您想查看更多详细信息(属性,…),可以在XAML代码中优化模板。

是否使用xml序列化?@TraeMoore是的。我正在使用xml序列化
<Window.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="NodeTemplate">
            <Grid>
                <TextBlock Text="{Binding Path=Name}"/>
            </Grid>
        </HierarchicalDataTemplate>
</Window.Resources>
<TreeView x:Name="myTreeView" Grid.Column="0"
      ItemsSource="{Binding Path=Root.Elements}" 
      ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
      />
private XDocument _theXML;
public XDocument TheXML {
    get => _theXML;
    set => _theXML = value;
}
public MainWindow()
{
    ...
    InitializeComponent();
    DataContext = this;
    TheXML = XDocument.Load(@"c:\file.xml");
    myTreeView.DataContext = TheXML;
    myTreeView.UpdateLayout();
}