XAML form + Cancel button response

I am working on my first form in Powershell and am using XAML (built on the code from https://foxdeploy.com/2015/04/10/part-i-creating-powershell-guis-in-minutes-using-visual-studio-a-new-hope/).

The form works great so now I’m working on data validation. I am trying to make all of the fields mandatory through a While loop, but I can’t break out of it. How would I take the code below and make it understand when someone clicks the Cancel button, that it should exit the loop? I tried checking $WPFcancelButton.IsPressed, but that doesn’t seem to be getting updated.

Thanks.

Function Get-HubNameFields { $inputXML = @" <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="Naming Tool" Height="350" Width="525"> <Grid Margin="0,0,-8,0" HorizontalAlignment="Left" Width="525"> <TextBlock x:Name="textBlock" Margin="10,10,145.907,0" TextWrapping="Wrap" Text="Define the parameters of a name" VerticalAlignment="Top" Height="17.945"/> <Label x:Name="custLongNameLabel" Content="Customer Long Name" HorizontalAlignment="Left" Margin="10,32.945,0,0" VerticalAlignment="Top"/> <TextBox x:Name="custLongNameTxt" Height="21.96" Margin="141.003,36.945,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Full name of the customer."/> <Label x:Name="custShortNameLabel" Content="Customer Short Name" HorizontalAlignment="Left" Margin="10,59.905,0,0" VerticalAlignment="Top"/> <TextBox x:Name="custShortNameTxt" Height="21.96" Margin="141.003,63.905,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Abreviation of the customer's name."/> <Label x:Name="siteLongNameLabel" Content="Site Long Name" HorizontalAlignment="Left" Margin="10,86.865,0,0" VerticalAlignment="Top"/> <TextBox x:Name="siteLongNameTxt" Height="21.96" Margin="141.003,90.865,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Full name of the site."/> <Label x:Name="siteShortNameLabel" Content="Site Short Name" HorizontalAlignment="Left" Margin="10,113.785,0,0" VerticalAlignment="Top"/> <TextBox x:Name="siteShortNameTxt" Height="21.96" Margin="141.003,117.825,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Abbreviation of the site's name."/> <Label x:Name="hubNumberLabel" Content="Hub Number" HorizontalAlignment="Left" Margin="10,140.825,0,0" VerticalAlignment="Top"/> <TextBox x:Name="hubNumberTxt" Height="21.96" Margin="141.003,144.785,224.239,0" TextWrapping="Wrap" VerticalAlignment="Top" ToolTip="Number of the Hub."/> <RadioButton x:Name="radioButton" Content="Data Center Hub" HorizontalAlignment="Left" Margin="10,0,0,122.611" VerticalAlignment="Bottom"/> <RadioButton x:Name="radioButton1" Content="Office/Other" HorizontalAlignment="Left" Margin="10,0,0,102.651" VerticalAlignment="Bottom"/> <Button x:Name="okButton" Content="OK" HorizontalAlignment="Left" Margin="18.928,0,0,10" VerticalAlignment="Bottom" Width="50" IsDefault="True"/> <Button x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="80.317,0,0,10" VerticalAlignment="Bottom" Width="50" IsCancel="True"/> </Grid> </Window> "@ $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' [void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework') [xml]$XAML = $inputXML #Read XAML $reader = (New-Object System.Xml.XmlNodeReader $xaml) Try { $form = [Windows.Markup.XamlReader]::Load($reader) } Catch { Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .Net is installed." } #This line takes the XAML data, adds "WPF" to the front of each XML node name, and creates a global variable. $xaml.SelectNodes("//*[@Name]") | Foreach {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -Scope Global} <#Only need this function and its call if you don't know the name of the variables that the function generates from the XML. Function Get-FormVariables { If ($global:ReadmeDisplay -ne $true) { Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true } Write-host "Found the following interactable elements from our form" -ForegroundColor Cyan Get-Variable WPF* } Get-FormVariables#> #Specify what the OK button should do, when clicked. $WPFokButton.Add_Click({$form.Close()}) #Now that the form is built, show it, surpressing other messages. $form.ShowDialog() | Out-Null } #Initialize some local variables. $hubType = $null $TextInfo = (Get-Culture).TextInfo While ((($customerLongName.Length -eq 0) -or ($customerShortName.Length -eq 0) -or ($siteLongName.Length -eq 0) -or ($siteShortName.Length -eq 0) -or ($hubType -eq $null)) -or ($WPFcancelButton.IsPressed -like "false*")) { Write-Host "the cancel button value is $($WPFcancelButton.ispressed)" Get-HubNameFields $customerLongName = $WPFcustLongNameTxt.Text $customerLongName = $TextInfo.ToTitleCase($customerLongName.ToLower()) $customerLongName = $customerLongName -replace 's','' $customerShortName = ($WPFcustShortNameTxt.Text).ToLower() $customerShortName = $customerShortName -replace 's','' $siteLongName = $WPFsiteLongNameTxt.Text $siteLongName = $TextInfo.ToTitleCase($siteLongName.ToLower()) $siteLongName = $siteLongName -replace 's','' $siteShortName = ($WPFsiteShortNameTxt.Text).ToLower() $siteShortName = $siteShortName -replace 's','' If ($WPFradioButton.IsChecked -eq $true) { $hubType = 'dh' } If ($WPFradioButton1.IsChecked -eq $true) { $hubType = 'ch' } } 

submitted by /u/wetling
[link] [comments]

Leave a Reply