温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - WPF-C# StackPanel Visibility and ToggleButton

其他 - WPF-C#StackPanel可见性和ToggleButton

发布于 2020-03-27 15:52:38

我正在使用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?

查看更多

查看更多

提问者
Rei
被浏览
37
maulik kansara 2020-01-31 16:55

您正面临这样的问题,只是因为您的窗口允许检查多个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}但是,您需要检查。

发布
问题

分享
好友

手机
浏览

扫码手机浏览