Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically I need something with expander control.
I have a main expander. Under it it has many sections. What I want is to put the sections in a treeview. Each section also has its own expander. The main expander controls all. Which means if it collapses, all sections should be collapsed, vice versa.

The changeling part is that I want to set the background. I want the background of the expanders are different. When sections expand, the background of the sections are different from the main expander.

What I have tried:

I tried to use template to customize the expanders, but not sure how to trigger the background with the styles. I can't find the similar example online. Need help.
Posted
Updated 22-Mar-18 2:19am
Comments
Graeme_Grant 25-Nov-17 8:43am    
Have you looked at the TreeView template? They're already expanders, you only need to style them. You can see the template here: TreeView Styles and Templates | Microsoft Docs[^]
Member 12658724 25-Nov-17 10:18am    
Did you mean I have to style the ToggleButton? When I click it the style will be changed by some trigger?
Graeme_Grant 25-Nov-17 11:06am    
You have total control over the look and feel to achieve what you want. I do just that in the WpfFileExplorer sample in this article: Working with JSON in C# & VB[^]

1 solution

You can use the VisualTreeHelper:

private static IEnumerable<T> FindVisualChildren<T>(DependencyObject root) where T : DependencyObject
 {
     if (root != null)
         for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
         {
             DependencyObject child = VisualTreeHelper.GetChild(root, i);
             if (child is T) yield return (T)child;
             foreach (T childOfChild in FindVisualChildren<T>(child))
                 yield return childOfChild;
         }
 }



use the following

var treeItems = FindVisualChildren<treeviewitem> (this),ToList();
treeItems.ForEach(I => i.IsExpanded = false);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900