如何使用WPF从XML文件创建treeview?

时间:2010-02-19 08:21:16

标签: wpf treeview

这是XML文件

 <Root>
 <RootNode name="CurrentDbName" value="DeltaTest Backup" DesiPath="E:\BuildBackups">
 <ChildNode name="Application" value="App">
  <LeafNode name="Source" value="Source" SourcePath="E:\Alertv2" /> 
  <LeafNode name="Publish" value="Publish" SourcePath="C:\Alert_Source" /> 
  </ChildNode>
 <ChildNode name="Database" value="DB">
  <LeafNode name="Dev" value="Dev" SourcePath="C:\Kiran3" /> 
  <LeafNode name="Build" value="Build" SourcePath="C:\Kiran4" /> 
  </ChildNode>
  </RootNode>  </Root>

由此,我想在WPF中创建一个树视图,看起来像

-Root
 --DeltaTestBaclup
  ---App
    ----Source
    ----Publish
  ---Db
    ----Dev
    ----Build

所以请帮我创建这个树视图。

5 个答案:

答案 0 :(得分:2)

这是一种以编程方式执行此操作的方法。这基于this website's solution

public YourWindow()
{
    InitializeComponent();
    BuildTree(treeView, XDocument.Load(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), @"testxml.xml")));
}

private void BuildTree(TreeView treeView, XDocument doc)
{
    TreeViewItem treeNode = new TreeViewItem 
    {  
        //Should be Root
        Header = doc.Root.Name.LocalName,
        IsExpanded = true
    };
    treeView.Items.Add(treeNode);
    BuildNodes(treeNode, doc.Root);
}

private void BuildNodes(TreeViewItem treeNode, XElement element)
{
    foreach (XNode child in element.Nodes())
    {
        switch (child.NodeType)
        {
            case XmlNodeType.Element:
                XElement childElement = child as XElement;
                TreeViewItem childTreeNode = new TreeViewItem
                {
                    //Get First attribute where it is equal to value
                    Header = childElement.Attributes().First(s => s.Name == "value").Value ,
                    //Automatically expand elements
                    IsExpanded = true
                };
                treeNode.Items.Add(childTreeNode);
                BuildNodes(childTreeNode, childElement);
                break;
            case XmlNodeType.Text:
                XText childText = child as XText;
                treeNode.Items.Add(new TreeViewItem { Header = childText.Value, });
                break;
        }
    }
}

后面的代码将根据您的规范构建树。这是XAML

<Grid>
    <TreeView Margin="20" Background="LightGray" x:Name="treeView" />
</Grid>

答案 1 :(得分:0)

欢迎使用stackoverflow,如果您可以发布一些示例xml - 这将有助于可视化您的工作方式。我认为您需要使用1个或更多HierarchicalDataTemplate

假设一个名为data.xml的xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<root>
     <item>
      <DeltaTestBaclup>aaa</DeltaTestBaclup>
      <App>bbb</App>
      <Source>ccc</Source>
      <Publish>ddd</Publish>
    </item>
    <item>
      <DeltaTestBaclup>aaa</DeltaTestBaclup>
      <App>bbb</App>
      <Source>ccc</Source>
      <Publish>ddd</Publish>
    </item>
</root>

您可以使用xaml类似于:

 <Grid>
        <Grid.DataContext>
            <XmlDataProvider Source="data.xml"></XmlDataProvider>
        </Grid.DataContext>
        <Grid.Resources>
            <HierarchicalDataTemplate x:Key="ItemTemplate" DataType="item">
                <TextBlock>
                    <TextBlock Text="{Binding XPath=DeltaTestBaclup}" />
                    <LineBreak></LineBreak>
                    <TextBlock Text="{Binding XPath=App}" />
                    <LineBreak></LineBreak>
                    <TextBlock Text="{Binding XPath=Source}" />
                    <LineBreak></LineBreak>
                    <TextBlock Text="{Binding XPath=Publish}" />                    
                </TextBlock>
            </HierarchicalDataTemplate>
        </Grid.Resources>

        <TreeView  Name="treeView" 
                   ItemsSource="{Binding Path=.,XPath=/root/item}" 
                   ItemTemplate="{StaticResource ItemTemplate}">      
        </TreeView>
    </Grid>

答案 2 :(得分:0)

我知道这个问题是在几个月前被问到的,但我认为没有准确回答,因为我遇到了同样的问题而这些建议都没有帮我解决。

这是一个链接,解释了如何使用winforms从.xml填充树视图:

http://www.codeproject.com/Articles/12606/Loading-and-Saving-XML-to-and-from-a-TreeView-Cont

我设法将其改编为wpf,以便使用.xml中的信息填充TreeView:

//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);

//Now, clear out the treeview, 
treeView.Items.Clear();

//and add the first (root) node
TreeViewItem treeviewItemRoot   = new TreeViewItem();
treeviewItemRoot.Header         = "Root";

treeView.Items.Add(treeviewItemRoot);

TreeViewItem tNode = new TreeViewItem();
tNode = (TreeViewItem)treeView.Items[0];

//We make a call to addTreeNode, 
//where we'll add all of our nodes
addTreeNode(xDoc.DocumentElement, tNode);



//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeViewItem treeNode)
{
    XmlNode xNode;
    TreeViewItem tNode;
    XmlNodeList xNodeList;
    if (xmlNode.HasChildNodes) //The current node has children
    {
        xNodeList = xmlNode.ChildNodes;
        for (int x = 0; x <= xNodeList.Count - 1; x++)
        //Loop through the child nodes
        {
            xNode = xmlNode.ChildNodes[x];

            treeNode.Items.Add(new TreeViewItem());
            tNode = treeNode.Items[x] as TreeViewItem;
            addTreeNode(xNode, tNode);
        }
    }
    else //No children, so add the outer xml (trimming off whitespace)
        treeNode.Header = xmlNode.OuterXml.Trim(); 
}

答案 3 :(得分:0)

    class Mapper
{
    private string sourceXmlFile;
    private XDocument xmlData;

    public Mapper(string xmlFilePath)
    {            
        sourceXmlFile = xmlFilePath;           
    }

    private void BuildNodes(TreeViewItem treeNode, XElement element)
    {

        string attributes = "";
        if (element.HasAttributes)
        {
            foreach (var att in element.Attributes())
            {
                attributes += " " + att.Name + " = " + att.Value;
            }
        }

        TreeViewItem childTreeNode = new TreeViewItem
        {
            Header = element.Name.LocalName + attributes,
            IsExpanded = true
        };
        if (element.HasElements)
        {
            foreach (XElement childElement in element.Elements())
            {
                BuildNodes(childTreeNode, childElement);
            }
        }
        else
        {
            TreeViewItem childTreeNodeText = new TreeViewItem
            {
                Header = element.Value,
                IsExpanded = true
            };
            childTreeNode.Items.Add(childTreeNodeText);                    
        }

        treeNode.Items.Add(childTreeNode);
    }



    public void LoadXml(TreeView treeview)
    {
        try
        {
            if (sourceXmlFile != null)
            {
                xmlData = XDocument.Load(sourceXmlFile, LoadOptions.None);
                if (xmlData == null)
                {
                    throw new XmlException("Cannot load Xml document from file : " + sourceXmlFile);
                }
                else
                {
                    TreeViewItem treeNode = new TreeViewItem
                    {
                        Header = sourceXmlFile,
                        IsExpanded = true
                    };


                    BuildNodes(treeNode, xmlData.Root);
                    treeview.Items.Add(treeNode);
                }
            }
            else
            {
                throw new IOException("Xml file is not set correctly.");
            }
        }
        catch (IOException ioex)
        {
            //log
        }
        catch (XmlException xmlex) 
        {
            //log
        }
        catch (Exception ex)
        {
            //log
        }
    }

}

这是每个xml结构的最通用版本。例如:

    <Catalog>
  <Book id="bk101">
        <Author>Garcia, Debra</Author>
    <Title id="33">XML Developer's Guide</Title>
    <Genre>Computer</Genre>
    <Price>44.95</Price>
    <PublishDate>2000-10-01</PublishDate>
    <Description>An in-depth look at creating applications 
      with XML.</Description>
  </Book>
  <Book id="bk102">
    <Author>Garcia, Debra</Author>
    <Title>Midnight Rain</Title>
    <Genre>Fantasy</Genre>
    <Price>5.95</Price>
    <PublishDate>2000-12-16</PublishDate>
    <Description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</Description>
  </Book>
</Catalog>

会产生这个: This how it looks

答案 4 :(得分:-1)

您可以阅读该here,该网站的一个示例:

<TreeView Margin="10,10,0,13" Name="TreeView1" HorizontalAlignment="Left"
 VerticalAlignment="Top" Width="194" Height="200">
    <TreeViewItem Header="Cold Drinks">
        <TreeViewItem Header="Coke"></TreeViewItem>
        <TreeViewItem Header="Pepsi"></TreeViewItem>
        <TreeViewItem Header="Orange Juice"></TreeViewItem>
        <TreeViewItem Header="Milk"></TreeViewItem>
        <TreeViewItem Header="Iced Tea"></TreeViewItem>
        <TreeViewItem Header="Mango Shake"></TreeViewItem>
    </TreeViewItem>
</TreeView>

这会导致这个

alt text http://www.c-sharpcorner.com/UploadFile/mahesh/WPFTreeView08202008231544PM/Images/WPFTreeViewImg1.jpg

所以在你的情况下,你需要添加一些TreeViewItems并将它们嵌套一点,使用上面的代码来获得你想要的结果!