How do you get the level of a treeviewitem in WPF C#? In windows forms there is a .Level member of the treeview class but there does not seem to be one for WPF C#.
A:
Build a view model.
A View model gves you greater flexibility with the treeview than you can achieve without it. Do yourself a favour, dont walk the visual tree, If a parent node is not visible, it could be virtualised away and your level (or depth) figure will be wrong. build a view model that wraps your data and knows at what level it is at.
Check out the answers posted here.
answer link one (you would add another property to your view model - level)
Aran Mulholland
2010-07-05 23:57:40
That looks pretty extensive.While I am working on this, let me ask you if a simpler operation is available without building a view model.In WPF, I can obtain what tree item the user has clicked on. After I do this, I can use the "items" member to get a list of children of that tree item. Is there a way of gaining a link to the parent? There is some sort of "inherited from" and "decended from" member of the treeviewitem class. How can I make use of that?
xarzu
2010-07-06 16:57:53
have a look at the link under your question, it involves walking the visual tree. Usually the item that you are looking for will be the DataContext of the TreeViewItem.
Aran Mulholland
2010-07-07 00:22:16