I am using C# and WPF to write a program which has a side menu like the one in the screenshot below.
The XAML Code for Menu Toggle Button 4 and 5 looks like this:
<ToggleButton x:Name="MenuBtn_4" Content="MENU TOGGLE BUTTON 4" />
<StackPanel x:Name="Submenu_4" Visibility="{Binding ElementName=MenuBtn_4,
Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter},
FallbackValue=Visible}">
<Button Content="SUBMENU BUTTON 1" />
<Button Content="SUBMENU BUTTON 2" />
</StackPanel>
<ToggleButton x:Name="MenuBtn_5" Content="MENU TOGGLE BUTTON 5" />
<StackPanel x:Name="Submenu_5" Visibility="{Binding ElementName=MenuBtn_5,
Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter},
FallbackValue=Visible}">
<Button Content="SUBMENU BUTTON 1" />
<Button Content="SUBMENU BUTTON 2" />
</StackPanel>
Right now if I click on the MenuToggleButton
, the StackPanel
with Submenu Buttons below becomes visible. If I click the ToggleButton
again it collapses, which is what I want.
But if the StackPanel
of one Menu ToggleButton
is visible and I click on another ToggleButton
the first one stays visible. I would like it so that when another Menu ToggleButton
is checked, the previously visible stack panels of the other Menu toggle buttons collapse. I tried solving this with MultiBinding
but I cannot seem to make it work. Does anyone have any ideas how I could do this?
You are facing such issue just because your window allows to check multiple ToggleButtons
. Your issue can be resolved by implementing a logic which allows only one ToggleButton
to be selected at a time.
And for this, you can use RadioButton
which looks like ToggleButton in UI.
<RadioButton x:Name="MenuBtn_4" Content="MENU TOGGLE BUTTON 4" GroupName="grp1"
Style="{StaticResource {x:Type ToggleButton}}" cal:Message.Attach="MenuSelect"/>
<StackPanel x:Name="Submenu_4" Visibility="{Binding ElementName=MenuBtn_4,
Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter},
FallbackValue=Visible}">
<Button Style="{StaticResource MenuBtn}" Content="SUBMENU BUTTON 1"
cal:Message.Attach="SubmenuSelect"/>
<Button Style="{StaticResource MenuBtn}" Content="SUBMENU BUTTON 2"
cal:Message.Attach="SubmenuSelect"/>
</StackPanel>
<RadioButton x:Name="MenuBtn_5" Content="MENU TOGGLE BUTTON 5" GroupName="grp1"
Style="{StaticResource {x:Type ToggleButton}}" cal:Message.Attach="MenuSelect"/>
<StackPanel x:Name="Submenu_5" Visibility="{Binding ElementName=MenuBtn_5,
Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter},
FallbackValue=Visible}">
<Button Style="{StaticResource MenuBtn}" Content="SUBMENU BUTTON 1"
cal:Message.Attach="SubmenuSelect"/>
<Button Style="{StaticResource MenuBtn}" Content="SUBMENU BUTTON 2"
cal:Message.Attach="SubmenuSelect"/>
</StackPanel>
NOTE: if you can see, I have used style {StaticResource {x:Type ToggleButton}}
but I think it should also work with your custom style {StaticResource ToggleMenuBtn}
. But that you need to check.
Thanks! Worked like a charm. I just changed my ToggleButtons to RadioButtons and added the GroupName. I did not know RadioButtons worked like that.