Partager via


Comment : modifier la propriété Visibility

Mise à jour : novembre 2007

Exemple

Cet exemple montre comment modifier la valeur de la propriété Visibility.

Le premier exemple, c'est-à-dire XAML (Extensible Application Markup Language), définit un TextBox et le place dans un élément StackPanel. Trois contrôles Button représentent les trois valeurs d'énumération de la propriété Visibility : Visible, Hiddenet Collapsed. Lorsqu'un utilisateur clique sur l'un de ces boutons, cela déclenche un événement dans le fichier code-behind associé au Button. L'état Visibility du TextBox est modifié afin de correspondre à la valeur associée au 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>

Le fichier code-behind suivant gère les événements ButtonClick définis par l'exemple XAML précédent.

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.";
}

Voir aussi

Concepts

Vue d'ensemble de Panel

Référence

Visibility

Visibility