XAML GUI Button Variable Enumerate

I am using the following script to read a SQL DB table to populate a XAML GUI for table value editing. The part I am hung up on is getting variables defined in the PS1 to handle button click events. I have tried numerous ideas but for the life of me I cant seem to get this to work without doing an out-file in the foreach that creates other issues. In case this tinker script isnt enough info for each SQL table row the XAML creates a button row Name and Value columns, this part I have working using an enumerating list as it reads the rows. It’s the PS script part where i write in the add_click actions, tooltips, and other functions to the buttons that I cannot get to happen correctly.

 

[pre]

$i = 0

SQL Script ------------------------------------------------------------------------------------------------------------------------------------------------------------

$SQLScript = @"
SELECT Name, Value FROM MachineSetting
"@

SQL DB Query String ---------------------------------------------------------------------------------------------------------------------------------------------------

$SQLConnection = new-object system.data.sqlclient.sqlconnection( "Data Source=RENAMEDPC\SQLExpress;Initial Catalog=Machinery;uid=User;password=mypassword ”)
$SQLAdapter = new-object system.data.sqlclient.sqldataadapter ($SQLScript, $SQLConnection)
$SQLTable = new-object system.data.datatable
$SQLAdapter.Fill($SQLTable) | Out-Null
$DBTableArray = @($SQLTable)

Trial Script ----------------------------------------------------------------------------------------------------------------------------------------------------------

foreach ($EV in $DBTableArray.Value) {
$i += 1
$ButtonEnumerator = “`$Value$i”
$ButtonEnumerator.ToString() = $win.FindName(“Value$ButtonEnumerator”)
}

[/pre]

Curious to know, what did you expect from below line ?

$ButtonEnumerator.ToString() = $win.FindName("Value$ButtonEnumerator")

Not my best thought on what to try but what I am after is a way to have the foreach loop kick out all my variables. The one you ask about I was looking to see the following. At this point im spinning circles and making my problem worse tinkering, end result I want a foreach loop on both the Name and Value columns for the DB to populate an undetermined number of buttons and the functions for the add_click of each button.

 

[pre]

$Value1 = $win.FindName(“Value1”)

[/pre]

Here is the whole script, seems most all does what i want except the $NameListScript and $ValueListScript to form my PS variables to control the buttons. I do have some minor issues with button behaviors but I can work that out once I get them working with add_click events.

 

EDIT post seems to be cutting out the XAML but not sure if that is needed

 

[pre]

$ButtonEnumerator = 0
$Margin = -15
$NameListScript = New-Object System.Collections.Generic.List[System.Object]
$NameListXAML = New-Object System.Collections.Generic.List[System.Object]
$ValueListScript = New-Object System.Collections.Generic.List[System.Object]
$ValueListXAML = New-Object System.Collections.Generic.List[System.Object]

SQL Script ------------------------------------------------------------------------------------------------------------------------------------------------------------

$SQLScript = @"
SELECT Name, Value FROM MachineSetting
"@

SQL DB Query String ---------------------------------------------------------------------------------------------------------------------------------------------------

$SQLConnection = new-object system.data.sqlclient.sqlconnection( "Data Source=RENAMEDPC\SQLExpress;Initial Catalog=Machinery;uid=User;password=passwd ”)
$SQLAdapter = new-object system.data.sqlclient.sqldataadapter ($SQLScript, $SQLConnection)
$SQLTable = new-object system.data.datatable
$SQLAdapter.Fill($SQLTable) | Out-Null
$DBTableArray = @($SQLTable)

XAML and PS1 enumerated rows ------------------------------------------------------------------------------------------------------------------------------------------

foreach ($Name in $DBTableArray.Name) {
$ButtonEnumerator += 1
$Margin += 20
$NameListXAML.Add(“<Button Name=”“Text$ButtonEnumerator”" BorderBrush=“”#313130"" BorderThickness=““2"” Content=”“$Name”" HorizontalAlignment=““Left”” Margin=““5,$Margin,0,0"” VerticalAlignment=”“Top”" Width=““320"” />”)
$NameListScript.Add(“`$Text$ButtonEnumerator = `$win.FindName(”“Text$ButtonEnumerator”“);”)
$NameListScript.Add(“`$Text$ButtonEnumerator.Add_click({;”)
$NameListScript.Add(“`$Fill = `$Text$ButtonEnumerator.Content;”)
$NameListScript.Add(“`$Fill1 = `$Value$ButtonEnumerator.Content;”)
$NameListScript.Add(“`$OptionName.text = `$Fill;”)
$NameListScript.Add(“`$OptionValue.text = `$Fill1;”)
$NameListScript.Add(“`$ClickedName = ““`$Text$ButtonEnumerator””;”)
$NameListScript.Add(“`$ClickedValue = ““`$Value$ButtonEnumerator””});”)
}

$ButtonEnumerator = 0
$Margin = -15

foreach ($EV in $DBTableArray.Value) {
$ButtonEnumerator += 1
$Margin += 20
$ValueListXAML.Add(“<Button Name=”“Value$ButtonEnumerator”" BorderBrush=“”#313130"" BorderThickness=““2"” Content=”“$EV”" HorizontalAlignment=““Left”” Margin=““340,$Margin,0,0"” VerticalAlignment=”“Top”" Width=““320"” />”)
$ValueListScript.Add(“`$Value$ButtonEnumerator = `$win.FindName(”“Value$ButtonEnumerator”“);”)
$ValueListScript.Add(“`$Value$ButtonEnumerator.ToolTip = ““Original Value $EV””;”)
}

$NameListScript.ToArray()
$NameListXAML.ToArray()
$ValueListScript.ToArray()
$ValueListXAML.ToArray()

XAML, and PS1 ending variables ---------------------------------------------------------------------------------------------------------------------------------------

[xml]$XAMLScript = @"
Window xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation” AllowsTransparency=“True” Opacity=“0.98”
Title=“MainWindow” WindowStartupLocation=“CenterScreen” Height=“800” Width=“700” ResizeMode=“NoResize” WindowStyle=“None” FontFamily=“DIN Pro” ShowInTaskbar=“False” Topmost=“True”
Grid Background=“#313130
Grid.RowDefinitions
RowDefinition Height=“*”
RowDefinition Height=“Auto”
Grid.RowDefinitions
ScrollViewer Grid.Column=“0” Grid.Row=“0” Height=“740” Width=“700” VerticalAlignment=“Top”
Grid
$NameListXAML
$ValueListXAML
Grid
ScrollViewer
Grid Background=“#313130” Height=“60” VerticalAlignment=“Bottom”
TextBox Name=“OptionName” IsReadOnly=“True” Background=“#313130” Foreground=“White” Text=“” VerticalAlignment=“Bottom” HorizontalAlignment=“Left” Height=“18” Width=“320” Margin=“5,0,0,35”
TextBox Name=“OptionValue” Text=“” VerticalAlignment=“Bottom” HorizontalAlignment=“Left” Height=“18” Width=“320” Margin=“340,0,0,35”
Button Name=“Exit” Content=“Exit” HorizontalAlignment=“Right” Height=“20” Margin=“0,0,5,5” VerticalAlignment=“Bottom” Width=“125” VerticalContentAlignment=“Center”
Button Name=“Apply” Content=“Apply” HorizontalAlignment=“Right” Height=“20” Margin=“0,0,140,5” VerticalAlignment=“Bottom” Width=“125” VerticalContentAlignment=“Center”
Grid
Grid
Window
"@

Add-Type -AssemblyName PresentationFramework, System.Drawing, System.Windows.Forms
$NR = (New-Object System.Xml.XmlNodeReader $XAMLScript)
$Win = [Windows.Markup.XamlReader]::Load($NR)

$SQLServer = “RENAMEDPC\SQLExpress”
$OptionName = $win.FindName(“OptionName”)
$OptionValue = $win.FindName(“OptionValue”)
$Apply = $win.FindName(“Apply”)
$Exit = $win.FindName(“Exit”)
$NameListScript
$ValueListScript
$Apply.add_click({
ApplyScript
$Fill1 = “$ClickedValue”
$OptionValue.text = “$Fill1”
$Border1 = “`$Text$ClickedName.BorderBrush = ““green”””
$Border2 = “`$Value$ClickedValue.BorderBrush = ““green”””
Invoke-Expression $Border1
Invoke-Expression $Border2
})
$Exit.add_click({
$win.Close()
})
Function ApplyScript () {
$ApplyOption = $OptionName.text
$ApplyValue = $OptionValue.text
$SQLScript = @"
USE Machinery
UPDATE MachineSetting SET Value=‘$ApplyValue’ WHERE Name=‘$ApplyOption’
"@
Invoke-Sqlcmd -ServerInstance `$SQLServer -Query `$SQLScript
}

$win.ShowDialog()

[/pre]

Yes, the XAML would be prudent.
No, this and many Q&A forums like this one will not allow XAML and the like.
So, post to gist, GitHub or other, and post a link.

Why are you doing this?

$NameListScript = New-Object System.Collections.Generic.List[System.Object]
$NameListXAML = New-Object System.Collections.Generic.List[System.Object]
$ValueListScript = New-Object System.Collections.Generic.List[System.Object]
$ValueListXAML = New-Object System.Collections.Generic.List[System.Object]

You should only need one of these and reuse it in your code.

I took the < and the >'s out of it so it shows here. Well I would think I need atleast two, one for the xaml list and one for the PS script list but will clean that up later.