Good day!

Interested in finding the item TreeViewItem in the TreeView.

I know that MSDN had a solution, but at the moment the blocks with the code are empty and I don’t know how to be :(

In WinForms, for example, you could

if(treeView.Nodes[0].Text == "smth") { //do smth } 

What about WPF?


According to the advice below, I began to comprehend the basics of MVVM, and the brain safely accumulated. I used to have data from the collection in the TreeView, and now I need to make an intermediate Tree in the ViewModel to which the TreeView binds to, or what?


If I understand correctly, you need to use banding for correct operation. There is a plug-in in vm (I made it DataContext of the window), which has two dictionary categories (dictionary), and one of the dictionaries almost every element has its own dictionary and needs to be output in this format :

- category 1
- dictionary item
- dictionary item
--- All dictionary elements of the dictionary element
- dictionary item
- category 2
- dictionary item

due to the fact that nesting is large, I’m poking around in the HierarchicalDataTemplate, but to no avail.

  • one
    With WPF, it is customary to use MVVM, so the search comes down to searching among the view model graph where the data itself lies, and not working with the control. - vitidev
  • one
    The previous version is non-empty. But the truth is to look better in the VM, and not in the controls. - VladD 5:09
  • @vitidev edited the question, can you tell? :) - Jane Doe
  • I have little understanding of your problem, but there is a saying "Any problem can be solved by introducing an additional level of abstraction, besides the problem of too many levels of abstraction." If you can not bind the original structure to the template, then you need to make a wrapper in the form of a view model that will give the necessary data for binding. And in general, make it a rule to create wrappers for view models for models. At first it seems to be unnecessary work, but in fact it is almost always necessary that ReadOnly is not vitidev
  • @vitidev thanks for the hint, I will try. - Jane Doe

1 answer 1

Consider ways to work.

Direct tree filling

Passing through our data, we programmatically populate the tree, create nodes and fill them with data.

You can find the desired data node like this:

  • We run through the tree control as you ran in WinForms, pick up the controls and look at the data. Very uncomfortable, because the template can be any node. Change something and the search will have to be repaired.
  • We are looking for in the source data for which the tree was built. We find. But how to know the node? This means that besides the data, it is necessary to store the correspondence "data item <-> node in the tree". That is, we create a map of data mapping to the visual node of the tree.

We get a link to the tree node control and then we can do something with it.

MVVM option

In MVVM, it is not customary to pick hands with needlessly, but to make view models and trust WPF using the template and binding mechanism to visually display these view models.

therefore, for we build a logical tree of data, and WPF will create its visual projection using specified templates.

Moreover, after finding the data node (view model) one should not try to find out which control in the visual tree this node displays. Viewmodel does not have a WPF link. You need to change something inside this found view model, and WPF will monitor this and display the changes in the visual part.

  • Is it possible to have some specifics? How to build a logical tree from the data in the collection? - Jane Doe
  • @JaneDoe well, if with your hands, then professorweb.ru/my/WPF/UI_WPF/level22/22_6.php - vitidev
  • Hands are not quite right, is not it? In the comments, limited features, so added text to the question. - Jane Doe
  • @JaneDoe there is no such thing "right". Everyone builds a tree as he likes. I use MVVM everywhere, but if I need to fix something with my hands, I don’t shy away from the code in the code-behind and my controls and attachments, etc., where the code is clean. Your initial question is not about MVVM (in which it is easy to build - there are lots of examples in the network), and not even about building a tree, but about searching in a tree. - vitidev
  • understood you It is just logical to build first, and then look for it :) That's why I started asking. Thank. - Jane Doe