Skip to content

Instantly share code, notes, and snippets.

@indented-automation
Last active June 8, 2022 15:39
Show Gist options
  • Save indented-automation/f91575629c7a974d81306baa2e3d3e56 to your computer and use it in GitHub Desktop.
Save indented-automation/f91575629c7a974d81306baa2e3d3e56 to your computer and use it in GitHub Desktop.
Add-Type -AssemblyName PresentationFramework
[Xml]$xaml = '<?xml version="1.0" encoding="utf-8"?>
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="500" Width="500">
<DockPanel>
<Label Content="Title" DockPanel.Dock="Top" />
<Label Content="Copyright" Margin="5" DockPanel.Dock="Bottom" />
<StackPanel DockPanel.Dock="Left">
<Button Name="Button1" Margin="5,5,5,5" Content="Button1" />
<Button Name="Button2" Margin="5,0,5,5" Content="Button2" />
</StackPanel>
<ComboBox Margin="5" DockPanel.Dock="Top" />
<Grid Margin="5" DockPanel.Dock="Top">
<Label Name="OutputText" />
<ListView Name="OutputList" Visibility="Collapsed">
<ListView.View>
<GridView />
</ListView.View>
</ListView>
</Grid>
</DockPanel>
</Window>'
$xmlNodeReader = [System.Xml.XmlNodeReader]::new($xaml)
$Window = [System.Windows.Markup.XamlReader]::Load($xmlNodeReader)
# Handler for Button1 click
$Window.FindName("Button1").Add_Click( {
param ( $sender, $eventArgs )
$sender.FindName("OutputList").Visibility = 'Collapsed'
$sender.FindName("OutputText").Visibility = 'Visible'
$sender.FindName("OutputText").Content = "Button1 was pressed"
} )
# Handler for Button2 click
$Window.FindName("Button2").Add_Click( {
param ( $sender, $eventArgs )
$sender.FindName("OutputText").Visibility = 'Collapsed'
$values = Get-PSDrive
$ListView = $sender.FindName("OutputList")
$ListView.Visibility = 'Visible'
foreach ($property in $values[0].PSObject.Properties) {
$column = [System.Windows.Controls.GridViewColumn]@{
DisplayMemberBinding = [System.Windows.Data.Binding]::new($property.Name)
Header = $property.Name
Width = [Double]::NaN
}
$ListView.View.Columns.Add($column)
}
$ListView.ItemsSource = $values
} )
# Escape closes
$Window.Add_KeyDown( {
param ( $sender, $eventArgs )
if ($eventArgs.Key -eq 'ESC') {
$Window.Close()
}
})
$Window.ShowDialog()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment