PowerShell & WPF: Display or Hide Controls
https://www.systanddeploy.com/2019/12/powershell-wpf-display-or-hide-controls.html
In this short post, I will show you how to display or hide easily a control using PowerShell and WPF.
With XAML, many things can be done using control attributes. For instance the attribute Visibility allows you to hide or display a control.
See below the different values of this attribute and how to use them.
- Visible: Control is displayed
- Hidden: Control is hidden
- Collapsed: Control is hidden with no blank space
The Visibility attribute is really easy to apply to a control.
For instance, you have a button $MyButton.
To display it just configure it like this: $MyButton.Visibility = "Visible"
Now to hide it use: $MyButton.Visibility = "Hidden"
In my case I always use the Visibility attribute directly on StackPanel.
This way you can manage the visibility on a group of controls.
In the below GUI I have three RadioButtons
- Visible
- Hidden
- Collapsed
I have three groups of controls that contains each one Button and one TextBox.
See below the XAML used in this example:
As you may noticed I set a name at each StackPanel containing a combo Button and TextBox.
Now see below what I want:
1 / By clicking on Visible every controls are displayed
2 / By clicking on Hidden only the Button 3 and TextBox 3 are displayed
3 / By clicking on Hidden only the Button 3 and TextBox 3 are displayed
Depending of the selected button, the StackPanel will get the appropriate value (visible, hidden or collapsed).
See below the Powershell used to do that:
See below the result of the Visibility behavior depending of its value.
As you may noticed there is a difference between the Hidden and Collapsed controls.
The Hidden attribute will hide our controls as below:
The Collapsed attribute will hide our controls as below:
Now we understand how the collapsed value is cool.
Indeed, if you set the Visibility attribute to hidden, there is a blank space.
If you set the Visibility attribute to Collapsed, the blank space disappears and the third GroupBox will go up.
Enregistrer un commentaire