I'm trying to use WPF to display a window for user to enter a password in my PowerShell script, the window works but buttons do not respond...
The main window (which also use WPF) works fine, I only have this strange behavior with this one.
XAML code :
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
MinWidth="200" MaxWidth="400" Width="300"
MinHeight="100" MaxHeight="120" Height="110"
FontFamily="Segoe UI" FontSize="13">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Name="Label" Grid.Row="0" Content="Enter vault master key:" VerticalContentAlignment="Center" Margin="10,0,0,0"/>
<PasswordBox Name="Input" Grid.Row="1" BorderBrush="Gray" BorderThickness="0.5" Margin="10,5,10,5" />
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Name="Ok" Content="Ok" Width="70" Height="20" Margin="0,0,5,0"/>
<Button Name="Exit" Content="Exit" Width="70" Height="20" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</Window>
PowerShell code :
[xml]$xaml = (Get-Content -Path $Files.Inp)
$xamlReader = New-Object System.Xml.XmlNodeReader $xaml
$UserInput = @{}
$UserInput.Window = [Windows.Markup.XamlReader]::Load($xamlReader)
$Controls = [regex]::Matches((Get-Content $Files.Inp -Raw),'Name="(\w+)"')
$Controls.Groups | Where-Object Name -eq 1 | ForEach-Object{ $UserInput.($_.Value) = $UserInput.Window.FindName($_.Value) }
$UserInput.Window.ShowDialog() | Out-Null
$UserInput.Ok.Add_Click({
$UserInput.Input.Password
$UserInput.Window.Close()
})
$UserInput.Exit.Add_Click({ $UserInput.Window.Close() })
When I run the script, the window display and I can write in the password box but both button (Ok/exit) do not work (nothing append when I click on). I checked controls name and method I use (Add_Click), all seems right...
Can you help me ?
you have to move:
$UserInput.Window.ShowDialog() | Out-Null
to the bottom of your script