2015-09-29 53 views
0

我有六个堆栈面板元素,它们都是通过单独的切换按钮切换的,这是我需要的。但是,当用户打开其中一个堆叠面板时,我想关闭其他所有堆叠面板,只显示他们点击的那个面板。WPF在6个堆栈面板之间切换可见性,并在打开时隐藏所有其他面板

<Grid HorizontalAlignment="Left" Height="504" Margin="-1,1,0,0" VerticalAlignment="Top" Width="760"> 

    <Grid.Resources> 
     <BooleanToVisibilityConverter x:Key="boolConverter" /> 
    </Grid.Resources> 

    <StackPanel HorizontalAlignment="Left" Height="505" VerticalAlignment="Top" Width="124" Background="#FF45B8F9" Margin="1,-1,0,0"/> 

    <StackPanel HorizontalAlignment="Left" Height="505" VerticalAlignment="Top" Width="124" Background="#FFF95545" Margin="636,0,0,-1" Visibility="{Binding ElementName=button, Path=IsChecked, Converter={StaticResource boolConverter}}"/> 

    <StackPanel HorizontalAlignment="Left" Height="505" VerticalAlignment="Top" Width="124" Background="#FFF9F945" Margin="507,-1,0,0" Visibility="{Binding ElementName=button_Copy, Path=IsChecked, Converter={StaticResource boolConverter}}"/> 

    <StackPanel HorizontalAlignment="Left" Height="505" VerticalAlignment="Top" Width="124" Background="#FF76F945" Margin="378,-1,0,0" Visibility="{Binding ElementName=button1, Path=IsChecked, Converter={StaticResource boolConverter}}"/> 

    <ToggleButton x:Name="button" Content="Green" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="105" Click="button_Click"/> 

    <ToggleButton x:Name="button_Copy" Content="Yellow" HorizontalAlignment="Left" Margin="10,55,0,0" VerticalAlignment="Top" Width="105" Click="button_Click"/> 

    <ToggleButton x:Name="button1" Content="Red" HorizontalAlignment="Left" Margin="10,114,0,0" VerticalAlignment="Top" Width="105"/> 

</Grid> 
+0

你真的应该使用RowDefinitions和ColumnDefinitions一格。你说你有6个stackpanels,但我只看到4.另外,togglebuttons的数量不等于stackpanels的数量(虽然我认为每个stackpanel应该有每个togglebutton)。 – Hopeless

+0

我保持浓缩我认为应用于3的解决方案可以应用于3个面板。其中一个面板是主控面板。 –

回答

1

做一个单选按钮自定义模板,显示一个类似的扩展:

<Style TargetType="{x:Type RadioButton}"> 
    <Setter Property="GroupName" Value="MyToggleButtonGroupName"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type RadioButton}"> 
       <Expander IsExpanded="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked}"> 
        <ContentPresenter/> 
       </Expander> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<RadioButton> 
    <StackPanel>...</StackPanel> 
</RadioButton> 
<RadioButton> 
    <StackPanel>...</StackPanel> 
</RadioButton> 
<RadioButton> 
    <StackPanel>...</StackPanel> 
</RadioButton>