如何从节点WPF树视图中获取项目?

时间:2010-09-10 05:21:04

标签: c# wpf xaml

的TreeView:

   -MainNode
      -node0
          data01
          -data02
             data021
             data022
          data03
      -node1
          data11
      -node2
          data21

EventSelectedChange;

选择包含子节点的节点来获取最终数据?

例如: 选定节点node0接收数据(data01,data021,data022,data03)

4 个答案:

答案 0 :(得分:3)

我假设你是什么: 单击顶级节点(node0,node1,node2),我想获取树中的所有子数据节点。

如果这是您所需要的,请输入以下代码:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TreeView x:Name="myTreeView" Height="271" HorizontalAlignment="Left" Margin="47,12,0,0" VerticalAlignment="Top" Width="236" SelectedItemChanged="myTreeView_SelectedItemChanged">
        <TreeViewItem Header="node0" >
            <TreeViewItem Header="data01" />
            <TreeViewItem Header="data02" >
                <TreeViewItem Header="data021" />
                <TreeViewItem Header="data022" />
            </TreeViewItem>
            <TreeViewItem Header="data03" />
        </TreeViewItem>
        <TreeViewItem Header="node1" >
            <TreeViewItem Header="data11" />
        </TreeViewItem>
        <TreeViewItem Header="node2" >
            <TreeViewItem Header="data21" />
        </TreeViewItem>
    </TreeView>
</Grid>
</Window>

处理SelectedItemChanged事件后面的cs代码将如下所示

    private void myTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        foreach (TreeViewItem dataNode in ((TreeViewItem)e.NewValue).Items)
        {
            TraverseChildrenData(dataNode);
        }
    }

    public void TraverseChildrenData(TreeViewItem treeViewItem)
    {
        //do whatever you want to do with child data item here..   
        MessageBox.Show(treeViewItem.Header.ToString());

        foreach (TreeViewItem child in treeViewItem.Items)
        {
            TraverseChildrenData(child);
        }
    }

答案 1 :(得分:1)

我会使用简单的递归来获得你想要的东西。下面的伪代码:

private List recursive_method(Node selectedNode){

List<Node> data = new List<Node>();

foreach(child in nodelist.Children) {
    data.add(child);
    List<Node> children = recursive_method(child);
    if(children != null) data.addRange(children);
}

return data;

}

答案 2 :(得分:1)

要从节点结构中获取IEnumerable,我发现这篇文章非常有帮助

http://www.claassen.net/geek/blog/2009/06/searching-tree-of-objects-with-linq.html

它使用扩展方法,您可以获得所有可重用性和延迟执行的商品。

用法示例:

IEnumerable<XmlElement> nodes = treeViewItem.AsBreadthFirstEnumerable(x => x.ChildNodes);

答案 3 :(得分:0)

这是我之前回答的更改版本。这个使用TreeView的ItemsSource来绑定数据

XML数据:

<data>
  <datanode Header="node0" >
    <datanode Header="data01" />
    <datanode Header="data02" >
      <datanode Header="data021" />
      <datanode Header="data022" />
    </datanode>
    <datanode Header="data03" />
  </datanode>
  <datanode Header="node1" >
    <datanode Header="data11" />
  </datanode>
  <datanode Header="node2" >
    <datanode Header="data21" />
  </datanode>
</data>

XAML:

<Grid>
    <Grid.DataContext>
        <XmlDataProvider Source="Data.xml" XPath="/data/datanode" />
    </Grid.DataContext>
    <TreeView x:Name="myTreeView" Height="271" HorizontalAlignment="Left" Margin="47,12,0,0" VerticalAlignment="Top" Width="236" SelectedItemChanged="myTreeView_SelectedItemChanged" ItemsSource="{Binding}" >
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding}" >
                    <TextBlock Text="{Binding XPath=@Header}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Grid>

CS:

    private void myTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        foreach (XmlElement dataNode in ((XmlElement)e.NewValue).ChildNodes)
        {
            TraverseChildrenData(dataNode);
        }
    }

    public void TraverseChildrenData(XmlElement treeViewItem)
    {
        //do whatever you want to do with child data item here..   
        MessageBox.Show(treeViewItem.Attributes["Header"].Value);

        foreach (XmlElement child in treeViewItem.ChildNodes)
        {
            TraverseChildrenData(child);
        }
    }