我正在使用C#和WPF编写一个程序,该程序具有一个侧面菜单,如下面的屏幕快照所示。
菜单切换按钮4和5的XAML代码如下所示:
<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?
您正面临这样的问题,只是因为您的窗口允许检查多个ToggleButtons
。您的问题可以通过实施一种逻辑来解决,该逻辑一次ToggleButton
只能选择一个。
为此,您可以RadioButton
在UI中使用类似于ToggleButton的按钮。
<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>
注意:如果可以看到,我已经使用样式,{StaticResource {x:Type ToggleButton}}
但是我认为它也应该与您的自定义样式一起使用{StaticResource ToggleMenuBtn}
。但是,您需要检查。
谢谢!像魅力一样工作。我只是将ToggleButtons更改为RadioButtons并添加了GroupName。我不知道RadioButtons那样工作。