wpf 自定义treeview 如何获得树节点集合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf 自定义treeview 如何获得树节点集合相关的知识,希望对你有一定的参考价值。
参考技术A 给段我改写的代码,编译通过的。数据类:
public class LeafNode
private string data;
public string Data
get return data;
set data = value;
private List<LeafNode> children;
public List<LeafNode> Children
get return children;
set children = value;
前台代码:
<Window.Resources>
<HierarchicalDataTemplate DataType="x:Type local:LeafNode" ItemsSource="Binding Path=Children">
<TextBlock Text="Binding Path=Data" ToolTip="Binding Path=Data"/>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView x:Name="twLeaf"/>
<Button Name="btn" Click="btn_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="5" Width="50" Height="25" Content="click"/>
</Grid>
后台代码:
List<LeafNode> roots = new List<LeafNode>();
List<LeafNode> roots1 = new List<LeafNode>();
public test3()
InitializeComponent();
for (int i = 0; i < 5; i++)
LeafNode child = new LeafNode() Data = "root" + i.ToString() ;
child.Children = new List<LeafNode>();
roots.Add(child);
for (int j = 0; j < 10; j++)
LeafNode subchild = new LeafNode() Data = "son" + j.ToString() ;
subchild.Children = new List<LeafNode>();
child.Children.Add(subchild);
for (int k = 0; k < 15; k++)
LeafNode gs = new LeafNode() Data = "grantSon" + k.ToString() ;
subchild.Children.Add(gs);
twLeaf.ItemsSource = roots;
private void btn_Click(object sender, RoutedEventArgs e)
roots1.Clear();
for (int i = 0; i < twLeaf.Items.Count; i++)
LeafNode rootItem = twLeaf.Items[i] as LeafNode;
if (rootItem != null)
GetTreeviewNodes(null,rootItem);
private void GetTreeviewNodes(LeafNode parentNode, LeafNode twItem)
LeafNode node = new LeafNode()
Data = twItem.Data,
Children = new List<LeafNode>()
;
if (parentNode != null)
parentNode.Children.Add(node);
else
roots1.Add(node);
if (twItem.Children!=null && twItem.Children.Count > 0)
for (int i = 0; i < twItem.Children.Count; i++)
LeafNode item = twItem.Children[i] as LeafNode;
if (null != item)
GetTreeviewNodes(node, item);
else
return;
说明:roots是给treeview赋值的,roots1是通过按钮事件获取treeview节点集合的。
C# treeview 获得选中值
treeview的CheckBoxes属性设为True
多选的,如何获得所选值?
求代码.
IList<string> list = new List<string>();//定义一个泛型集合
TreeNodeCollection nodeList = tvMiaoyin.CheckedNodes;//得到所有选中复选框的节点集合
foreach (TreeNode node in nodeList)//遍历集合,获得被选中节点的值
list.Add(node.Value);
参考技术A public void Getvalue()
String str[]=new ArrayList();//定义一个集合
TreeNode List = tvMiaoyin.CheckedNodes;//得到多选框中的值
foreach (TreeNode node in List)//遍历集合,获得被选中节点的值
list.Add(node.Value);
for(int i=0;i<list.size();i++)//测试一下是否得到了所选的值
System.out.print(list.get(i));
以上是关于wpf 自定义treeview 如何获得树节点集合的主要内容,如果未能解决你的问题,请参考以下文章
WPF TreeView - 如何在添加/删除节点后刷新树?