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]