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.