WPF change the Visibility property of UIElement in VB.NET

This article shows you how to change the visibility property of a UIElement in WPF.
  • 5865

Visibility property enables you to set the visibility of certain UIElement objects to different states like: Visible, Collapsed and Hidden. As a property, Visibility uses enumeration and not a boolean. This tutorial shows you how to use it and how to bind to Visibility property.

States of Visibility


Visible:
It simply displays the element itself.


Hidden:
Hidden property describes the state where UIElement (control itself) is not rendered on top of the application surface but its space stays reserved.


Collapse:
This completely removes the element and does not reserve space for it in UI layout.


Code to change the Visibility property of UIElement

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Visibility_Layout_Samp.Window1"
    Title="UIELement.Visibility Sample">
    <DockPanel>
        <StackPanel DockPanel.Dock="Left">
            <Button Name="btn1" Height="25" Click="contentVis" FontSize="12" 
             Foreground
="Sienna">Visibility="Visible"</Button>
            <Button Name="btn2" Height="25" Click="contentHid" FontSize="12" 
             Foreground
="Sienna">Visibility="Hidden"</Button>
            <Button Name="btn3" Height="25" Click="contentCol" FontSize="12" 
             Foreground
="Sienna">Visibility="Collapse"</Button>
        </StackPanel>
        <StackPanel HorizontalAlignment="Center">
            <TextBox Name="tb1" Width="116" Height="49" FontStyle="Italic" FontSize="12"  
             FontWeight
="Bold" Foreground="Blue">Here is your Text</TextBox>
            <TextBlock Name="txt1" TextWrapping="Wrap" FontSize="14"/>
        </StackPanel>
    </DockPanel>
</
Window>

VB Code

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Namespace Visibility_Layout_Samp
    Partial Public Class Window1
        Inherits Window
        Public Sub contentVis(ByVal sender As Object, ByVal e As RoutedEventArgs)
            tb1.Visibility = System.Windows.Visibility.Visible
            txt1.Text = "Visibility is now set to Visible."
        End Sub
         Public Sub contentHid(ByVal sender As Object, ByVal e As RoutedEventArgs)
            tb1.Visibility = System.Windows.Visibility.Hidden
            txt1.Text = "Visibility is now set to Hidden."
        End Sub
        Public Sub contentCol(ByVal sender As Object, ByVal e As RoutedEventArgs)
            tb1.Visibility = System.Windows.Visibility.Collapsed
            txt1.Text = "Visibility is now set to Collapse."
        End Sub
    End Class
End Namespace

Output Window

visibility1.gif
                               Figure 1

visibilty2.gif
                               Figure 2
Summary

Here we discussed about how to change the Visibility property of UIElement in WPF, I hope this will help you.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.