Having 4 Expander controls. When one expander is expanded how can I make all others collapse/close?
this is how I did it:
1) added a StackPanel and MUST add a name tag attribute (as this is the master).
StackPanel Name="StackPanel1"
2) add as many Expanders as you need (1 to 100's if needed) each MUST have:-
Expanded="Expander_Expanded"
added (notice all have 100% the same wording).
3) no other details need to match on each ( no height's names etc.. needed).
Xaml:
Expander 1
Expander 2
Expander 3
Expander 4
4) To control the open/close of all "Expanders" on the named "StackPanel1" StackPanel you only need to add the below code once.
VB code-behind:
Private Sub Expander_Expanded(sender As Object, e As RoutedEventArgs)
For Each exp As Expander In StackPanel1.Children
If exp IsNot sender Then
exp.IsExpanded = False
End If
Next
End Sub
5)Now you can change/add what content, button's, textbox's etc.. you need just do not change 2 things 1, "StackPanel Name" 2, "Expander Expanded" without updating the code-behind else things will not work.
Hope this information is helpful to you.
What's happening?
1) All panels are parents and all controls on that panel are children,
2) All controls are children of a parent panel.
3) A class deals with one call at a time.
4) The class deals with child.
6) The class move to next child.
7) Stops once all children have been asked.
So the pseudo code is like this:
1) Listen for a child’s named x
2) Ask each child in parents list of children
3) If child is not calling then
4) Child is expanded is false
5) End asking that child
6) Move to next child and ask again
7) Until all children have been asked