I´m working on a Gui project for a company and we are trying to create a navigational system using expanders and buttons.
The navagational pane is supposed to look something like this
Button
Expander
Button
Button
Expander
Button
Button
Button
The problem is that if i open expander it doesnt close if a push a button but it does close if i press another expander.
Combinding multiple buttons and expanders together requires you to synchronize all the controls.
You need the all buttons to close the open expander, and have the opening expander close the open expander.
You can do this from the code-behind fairly easily by naming the controls; however, to implement this
solution using MVVM (with binding) is a bit more tricky because the Expander IsExpanded property does not support two way binding.
The key is to implement a Converter used by the Expander against its IsExpanded state as follows:
Each button command sets the IsButtonSelected flag to true which closes the open Expander.
Because Expanders use a Converter you can use the ConvertBack method to close the open Expander from the opening Expander.
I’ve include the code to help you follow along.
Here is the View with 3 buttons and 2 Expanders:
Here is the Converter called from the Expander:
Here is the ViewModel:
I have not included the base class ViewModelBase and the Command code because you tagged MVVM so I’ll assume you have your own implementation for these.