Hello.

How to bypass the TreeView in WPF and display the text of the parent and child nodes? Moreover, to each node two more child nodes have been added. Terms and Subsections need to get the text of each such node and the text of nodes that are located in the Terms and Subsections nodes.

  • Item1
  • Terms
    • List item1
    • List item2
  • Subsections
    • Item1.1
    • Terms
      • List item3
      • List item4
    • Subsections
      • Item1.1.1
      • Terms
        • List item5
        • List item6
      • Subsections
        • Item1.1.1.1

Bypass Code:

foreach (TreeViewItem mPti in treeviewRazdel.Items) { System.Windows.MessageBox.Show(mPti.Header.ToString()); if (mPti.Items.Count != 0) { foreach (TreeViewItem pmxnt in mPti.Items) { System.Windows.MessageBox.Show(pmxnt.Header.ToString()); if (pmxnt.Header.ToString() == "Термины") { foreach (TreeViewItem dmx in pmxnt.Items) { System.Windows.MessageBox.Show(dmx.Header.ToString()); } } if (pmxnt.Header.ToString() == "Подразделы") { foreach (TreeViewItem pmx in pmxnt.Items) { System.Windows.MessageBox.Show(pmx.Header.ToString()); foreach (TreeViewItem pxx in pmx.Items) { if (pxx.Header.ToString() == "Подразделы") { foreach (TreeViewItem phk in pxx.Items) { System.Windows.MessageBox.Show(phk.Header.ToString()); } } } } } } } } 

This code works, but for me, TreeViewItems subsections and terms are created by a button and for new ones, the workaround will not work. How to create a TreeViewItem dynamically and while traversing in a loop and getting its text. It is advisable that you do not have to create a bunch of new foreach and TreeViewItem, and the traversal was done in a couple of cycles. Those. you need to separately get item1 and its nodes, separately item1.1 and its nodes, etc. until the end of the tree is something like a detour in depth.

  • Please specify how you will use the bypass data and in what form (array or list of names, classes). Agree that your example is very raw, nesting in general only on 2 levels. But really see my answer below. Tip: First you need to decide on your data type, or class (like MyTreeItem). - semenvx27
  • Yes crude example. Probably I will use an array, I did not create a class in advance. But the main problem is not in this, but in how during the crawl I distribute, divide and write to different XML files what I get from the tree, different terms, sections and subsections into different XML files with the names s1, s1-1, s1 -1-1, s1-2 etc? - Demon

1 answer 1

The overall picture for your task: Create a simple recursive function, lay in it the necessary logic. For example:

 void FullObhod() { foreach (TreeViewItem pmxnt in MyTreeView1.Items) Obhod(pmxnt); } void Obhod(TreeViewItem tw) { foreach (TreeViewItem pmxnt in tw.Items) { //Анализ текущего элемента //в принципе сама рекурсия Obhod(pmxnt); } 

If there is a need to add additional elements or something else during the crawl, I can help, write to semenvx27@yandex.ru

  • I wrote to you in the mail. - Demon