[急问]treeview 中如何访问子节点?

chin_chen 2010-07-12 05:40:50
treeview里面的itemtemplate和itemstyle都自己定制的,treeview.items返回的是我自己定制的类不是treeviewitem,然后我想从某一个节点往下遍历,该怎么办?谢谢
...全文
299 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpt_adt2010 2010-07-14
  • 打赏
  • 举报
回复
treeview的例子我做过,但是代码是网上找的,感觉没必要记住,用时复制粘贴就OK
Sunpire 2010-07-13
  • 打赏
  • 举报
回复
如果想从 Model 中去遍历,一般这个 Model 都会是“组合模式”,即每个 Node 会有类似于 ChildNodes 的集合属性,一般的通过递归,便可以实现对树的“广度优先”遍历 (搞不清是广度优先还是深度优先了,反正是通过递归来遍历)。

另外,如想从前台去遍历,在Silverlight 的扩展方法中,也提供了获取整个“后代”的 TreeViewItem 的方法,在
程序集 System.Windows.Controls.Toolkit.dll 的

namespace System.Windows.Controls
{
public static class TreeViewExtensions
{
//
// 摘要:
// Get the TreeViewItem containers of a TreeView.
//
// 参数:
// view:
// The TreeView.
//
// 返回结果:
// The TreeViewItem containers of a TreeView.
//
// 异常:
// System.ArgumentNullException:
// view is null.
public static IEnumerable<TreeViewItem> GetDescendantContainers(this TreeView view);


//
// 摘要:
// Get the descendant TreeViewItem containers of a TreeViewItem.
//
// 参数:
// item:
// The TreeViewItem.
//
// 返回结果:
// The descendant TreeViewItem containers of a TreeViewItem.
//
// 异常:
// System.ArgumentNullException:
// item is null.
public static IEnumerable<TreeViewItem> GetDescendantContainers(this TreeViewItem item);

//
// 摘要:
// Get the item of the parent container for a specified item.
//
// 参数:
// view:
// The TreeView containing the item.
//
// item:
// The child item.
//
// 返回结果:
// The item of the parent container for the specified item, or null if not found.
public static object GetParentItem(this TreeView view, object item);

}
}

此外还有如 CollapseAll() 等诸多实用的方法。
leon0905 2010-07-13
  • 打赏
  • 举报
回复
SelectedItem,如果你采用的是将整个控件绑定后台model的方式,SelectedItem就是后台的model
chin_chen 2010-07-13
  • 打赏
  • 举报
回复
比如说,我treeview中的每行都有个checkbox,是样式中定制的,然后我想通过这个checkbox找到本行treeview的item,该怎么找?
xnagameboy 2010-07-13
  • 打赏
  • 举报
回复
T-bag?
bob330464696 2010-07-13
  • 打赏
  • 举报
回复
565656565656546546
chin_chen 2010-07-13
  • 打赏
  • 举报
回复
还有个问题,是 我如何通过控件去取得其背后绑定的数据了?
jv9 2010-07-12
  • 打赏
  • 举报
回复
参考这篇:http://www.silverlightchina.net/html/developer/2009/1212/393.html
老Key 2010-07-12
  • 打赏
  • 举报
回复
lz
你头像太个性了。。

崇拜啊。。。。呵呵。。。
冰心的小屋 2010-07-12
  • 打赏
  • 举报
回复
假设你绑定TreeView的类为TreeNode,可以这样遍历

ObserCollection<TreeNode> collection = treeView.ItemSource as ObserCollection<TreeNode>;

Bingding(collection);

方法 可以遍历
private void Bingding(ObserCollection<TreeNode> collection p_list)
{
foreach(TreeNode node in collection)
{
TreeViewItem item = treeView.GetContainterFromItem(node);//
if(item.HasItem)
{
Bingding(item );
}
}
}

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧