在按钮上更改网格的可见性在Xaml中单击

时间:2017-12-05 09:15:22

标签: wpf vb.net xaml

我试图找到某种方式来点击一个按钮(菜单),它会改变网格(子菜单)的可见性,其中包含 XAML中的触发器。我是在vb.net上创建的,但我想知道在 XAML 中是否更容易做到?

Private Sub ShowSubmenu(SubmenuSettings e As Grid)
        Dim bk As Integer = VisualTreeHelper.GetChildrenCount(MainGrid)
        For i = 0 To bk - 1
            Dim g As Object = VisualTreeHelper.GetChild(MainGrid, i)
            If TypeOf g Is Grid Then g.Visibility = Visibility.Hidden
        Next
        SubmenuSettings.Visibility = Visibility.Visible
    End Sub

Private Sub Visibility(sender As Object)
        If sender Is btn1 Then
            ShowSubmenu(pdm1)
        End If
End Sub

2 个答案:

答案 0 :(得分:1)

您可以使用ToggleButton代替Button, 我们的想法是根据ToggleButtonIsChecked属性切换网格可见性。

<Grid  Height="100" Width="100" Background="Green">
   <Grid.Style>
      <Style TargetType="Grid">
          <Setter Property="Visibility" Value="Collapsed"/>
          <Style.Triggers>
             <DataTrigger Binding="{Binding IsChecked,ElementName=btnFirst}" Value="True">
                 <Setter Property="Visibility" Value="Visible"/>
              </DataTrigger>
          </Style.Triggers>
       </Style>
   </Grid.Style>
</Grid>

 <ToggleButton x:Name="btnFirst" Content="BtnFirst" Height="100" Width="100" />

答案 1 :(得分:0)

你可以给你的网格&#34; X:名称=&#34; gridName&#34; &#34;,然后在点击按钮时从代码执行&#34; gridName.Visibility = Visibility.Collapsed&#34; (如你所愿,折叠或隐藏)。 &#34;