Partilhar via


Como: Change the Visibility Property

Exemplo

Este exemplo mostra como modificar o valor da propriedade Visibility.

No primeiro exemplo, que é Extensible Application Markup Language (XAML), define um TextBox e posiciona-o em um elemento StackPanel. Três Button controles representam os valores de enumeração de três do Visibility propriedade: Visible, Hidden, e Collapsed. Quando um usuário clica em qualquer um desses botões, o clique dispara um evento no arquivo code-behind que é associado com o Button. O estado Visibility do TextBox é modificado para corresponder ao valor associado com o Button.

    <Border BorderBrush="Black" BorderThickness="2" Background="White">
        <DockPanel>
    
            <TextBlock FontSize="20" FontWeight="Bold" DockPanel.Dock="Top" Margin="0,0,0,10">UIElement.Visibility Sample</TextBlock>
            <TextBlock DockPanel.Dock="Top" Margin="0,0,0,10">Click the buttons below to manipulate the Visibility property of the TextBox below.</TextBlock>
            <StackPanel DockPanel.Dock="Left">
                <Button Name="btn1" Height="25" Click="contentVis">Visibility="Visible"</Button>
                <Button Name="btn2" Height="25" Click="contentHid">Visibility="Hidden"</Button>
                <Button Name="btn3" Height="25" Click="contentCol">Visibility="Collapsed"</Button> 
            </StackPanel>    
            <StackPanel HorizontalAlignment="Center">
                <TextBox Name="tb1" Width="100" Height="50">A TextBox</TextBox>
                <TextBlock Name="txt1" TextWrapping="Wrap" FontSize="14"/>
            </StackPanel>
        </DockPanel>
    </Border>

The following code-behind file handles the Button Click events that the previous XAML example defines.

Private Sub contentVis(ByVal sender As Object, ByVal args As RoutedEventArgs)

    tb1.Visibility = System.Windows.Visibility.Visible
    txt1.Text = "Visibility is now set to Visible."
End Sub

Private Sub contentHid(ByVal sender As Object, ByVal args As RoutedEventArgs)

    tb1.Visibility = System.Windows.Visibility.Hidden
    txt1.Text = "Visibility is now set to Hidden. Notice that the TextBox still occupies layout space."
End Sub

Private Sub contentCol(ByVal sender As Object, ByVal args As RoutedEventArgs)

    tb1.Visibility = System.Windows.Visibility.Collapsed
    txt1.Text = "Visibility is now set to Collapsed. Notice that the TextBox no longer occupies layout space."
End Sub
private void contentVis(object sender, RoutedEventArgs e)
{
    tb1.Visibility = System.Windows.Visibility.Visible;
    txt1.Text = "Visibility is now set to Visible.";
}

private void contentHid(object sender, RoutedEventArgs e)
{
    tb1.Visibility = System.Windows.Visibility.Hidden;
    txt1.Text = "Visibility is now set to Hidden. Notice that the TextBox still occupies layout space.";
}

private void contentCol(object sender, RoutedEventArgs e)
{
    tb1.Visibility = System.Windows.Visibility.Collapsed;
    txt1.Text = "Visibility is now set to Collapsed. Notice that the TextBox no longer occupies layout space.";
}

Consulte também

Conceitos

Panels Overview

Referência

Visibility

Visibility