@@ -20,14 +20,6 @@ Function ConvertTo-WPFGrid {
2020
2121 [switch ]$Refresh ,
2222
23- [Parameter (HelpMessage = " Control how grid lines are displayed" )]
24- [ValidateSet (" All" , " Horizontal" , " None" , " Vertical" )]
25- [ValidateNotNullOrEmpty ()]
26- [string ]$Gridlines = " All" ,
27-
28- [Parameter (HelpMessage = " Run this scriptblock to initialize the background runspace" )]
29- [scriptblock ]$InitializationScript ,
30-
3123 [Parameter (HelpMessage = " Load locally defined variables into the background runspace" )]
3224 [alias (" var" )]
3325 [string []]$UseLocalVariable ,
@@ -73,22 +65,20 @@ Function ConvertTo-WPFGrid {
7365 [int ]$Timeout = 0 ,
7466 [object []]$Data ,
7567 [scriptblock ]$cmd ,
76- [switch ]$Refresh ,
77- [string ]$Gridlines
68+ [switch ]$Refresh
7869 )
7970
8071 # It may not be necessary to add these types but it doesn't hurt to include them
8172 Add-Type - AssemblyName PresentationFramework
8273 Add-Type - AssemblyName PresentationCore
8374 Add-Type - AssemblyName WindowsBase
8475
85- # get maximum available working area on the screen
8676 $s = [System.Windows.SystemParameters ]::WorkArea
8777
8878 $form = New-Object System.Windows.Window
8979 $form.Title = $Title
90- $form.MaxHeight = $s.Height
91- $form.MaxWidth = $s.Width
80+ $form.MaxHeight = $s.Height - 500
81+ $form.MaxWidth = $s.Width - 500
9282
9383 $form.SizeToContent = [System.Windows.SizeToContent ]::WidthAndHeight
9484
@@ -115,7 +105,6 @@ Function ConvertTo-WPFGrid {
115105 $form.left = $s.width / 2 - $form.ActualWidth / 2
116106 $form.UpdateLayout ()
117107 $form.focus
118-
119108 })
120109
121110 $Form.Add_closing ( {
@@ -126,83 +115,56 @@ Function ConvertTo-WPFGrid {
126115 })
127116 # Create a grid to hold the datagrid
128117 $Grid = new-object System.Windows.Controls.Grid
118+ # New-Object System.Windows.Controls.StackPanel
129119
130120 $Grid.HorizontalAlignment = " stretch"
131121 $grid.VerticalAlignment = " stretch"
132122
123+
133124 # add buttons to close and manually refresh (Issue #34)
134125 $btnRefresh = New-Object System.Windows.Controls.Button
135126 $btnRefresh.Content = " Refresh Now"
136127 $btnRefresh.Height = 25
137128 $btnRefresh.Width = 80
138129 $btnRefresh.HorizontalAlignment = " left"
139130 $btnRefresh.VerticalAlignment = " Top"
140- $btnRefresh.Margin = " 15,10,0,0"
141- $btnRefresh.ToolTip = " click to refresh data from the command"
131+ $btnRefresh.Margin = " 10,10,0,0"
142132
143133 $btnRefresh.add_click ( {
144- [System.Windows.Input.Mouse ]::OverrideCursor = [System.Windows.Input.Cursors ]::Wait
145-
146- $form.Title = " $Title ...refreshing content. Please wait."
147-
148- $timer.stop ()
149- start-sleep - seconds 3
150-
151- $datagrid.itemssource = Invoke-Command - ScriptBlock $cmd
152-
134+ $datagrid.itemssource = Invoke-Command - ScriptBlock $cmd
135+ # $datagrid.items.refresh()
153136 foreach ($col in $datagrid.Columns ) {
154137 # because of the way I am loading data into the grid
155138 # it appears I need to set the sorting on each column
156139 $col.CanUserSort = $True
157140 $col.SortMemberPath = $col.Header
158141 }
159-
160142 $script :Now = Get-Date
161-
162- if ($script :count ) {
163- $script :count = $Timeout
164- [datetime ]$script :terminate = $now.AddSeconds ($timeout )
165- $ts = New-TimeSpan - Seconds $script :count
166- $status.text = " Last updated $script :Now - refresh in $ ( $ts.tostring ()) "
167- }
168- else {
169- $status.text = " last updated $Script :Now "
170- }
171- if ($refresh ) {
172- $timer.start ()
173- }
174- $form.title = $Title
175- [System.Windows.Input.Mouse ]::OverrideCursor = [System.Windows.Input.Cursors ]::Arrow
143+ $status.text = " last updated $Script :Now "
176144 })
177-
178145 $grid.AddChild ($btnRefresh )
179146
180147 $btnClose = New-Object System.Windows.Controls.Button
181- $btnClose.Content = " Close"
182- $btnClose.Height = 25
183- $btnClose.Width = 80
184- $btnClose.HorizontalAlignment = " Right"
185- $btnClose.VerticalAlignment = " Top"
186- $btnClose.Margin = " 0,10,15,0"
187- $btnClose.ToolTip = " close the form and quit"
188-
189- $btnClose.add_click ( {
190- $form.Close ()
191- })
192- $grid.AddChild ($btnClose )
148+ $btnRefresh.Content = Close
149+ $btnRefresh.Height = 25
150+ $btnRefresh.Width = 80
151+ $btnRefresh.HorizontalAlignment = " left"
152+ $btnRefresh.VerticalAlignment = " Top"
153+ $btnRefresh.Margin = " 10,10,0,0"
154+
155+
193156
194157 # create a datagrid
195158 $datagrid = New-Object System.Windows.Controls.DataGrid
196159 $datagrid.width = " Auto"
197160 $datagrid.Height = " Auto"
198161 $datagrid.VerticalAlignment = " stretch"
199162 $datagrid.HorizontalAlignment = " stretch"
200- $datagrid.margin = " 0,50,0,25 "
163+ $datagrid.margin = " 0,50,0,0 "
201164
202165 $datagrid.ColumnWidth = " Auto"
203- $datagrid .GridLinesVisibility = $Gridlines
166+
204167 $datagrid.VerticalScrollBarVisibility = [System.Windows.Controls.ScrollBarVisibility ]::Auto
205- $datagrid.HorizontalScrollBarVisibility = [System.Windows.Controls.ScrollBarVisibility ]::Auto
206168 $datagrid.CanUserSortColumns = $True
207169 $datagrid.CanUserResizeColumns = $True
208170 $datagrid.CanUserReorderColumns = $True
@@ -223,11 +185,13 @@ Function ConvertTo-WPFGrid {
223185 }
224186 $Grid.AddChild ($datagrid )
225187
188+
226189 $status = New-Object System.Windows.Controls.TextBlock
227190 $status.Height = 20
228191 $status.Background = " lightgray"
229192 $status.VerticalAlignment = " bottom"
230193 $status.HorizontalAlignment = " stretch"
194+ $btnRefresh.ToolTip = " click to refresh data from the command"
231195 $status.Width = " Auto"
232196
233197 $Grid.AddChild ($status )
@@ -244,23 +208,20 @@ Function ConvertTo-WPFGrid {
244208
245209 $timer.add_tick ( {
246210
247- $ts = new-timespan - seconds $script :count
248211 if ((Get-Date ) -lt $script :terminate -AND $Refresh ) {
249- $status.text = " Last updated $script :Now - refresh in $ ( $ts .tostring () ) "
212+ $status.text = " Last updated $script :Now - refresh in $script :count seconds "
250213 $script :count --
251214 }
252215 elseif ( (Get-Date ) -lt $script :terminate ) {
253216
254- $status.text = " Last updated $script :Now - closing in $ ( $ts .tostring () ) "
217+ $status.text = " Last updated $script :Now - closing in $script :count seconds "
255218 $script :count --
256219 }
257220 else {
258221 $timer.stop ()
259222 if ($Refresh ) {
260- $form.Title = " $Title ...refreshing content. Please wait."
261- [System.Windows.Input.Mouse ]::OverrideCursor = [System.Windows.Input.Cursors ]::Wait
262223 $datagrid.itemssource = Invoke-Command - ScriptBlock $cmd
263-
224+ # $datagrid.items.refresh()
264225 foreach ($col in $datagrid.Columns ) {
265226 # because of the way I am loading data into the grid
266227 # it appears I need to set the sorting on each column
@@ -270,11 +231,9 @@ Function ConvertTo-WPFGrid {
270231 $script :count = $timeout
271232 $script :now = Get-Date
272233 [datetime ]$script :terminate = $now.AddSeconds ($timeout )
273- $ts = new-timespan - Seconds $script :count
274- $status.text = " Last updated $ script :Now - refresh in $ ( $ts .tostring () ) seconds "
234+ $status .text = " Last updated $ script :Now - refresh in $script :count seconds "
235+ # $status.UpdateLayout()
275236 $Timer.Start ()
276- $form.title = $Title
277- [System.Windows.Input.Mouse ]::OverrideCursor = [System.Windows.Input.Cursors ]::Arrow
278237 }
279238 else {
280239 $form.close ()
@@ -290,7 +249,6 @@ Function ConvertTo-WPFGrid {
290249 }
291250
292251 If ($UseProfile ) {
293- Write-Verbose " Loading user profiles"
294252 $profiles = $profile.AllUsersAllHosts , $profile.AllUsersCurrentHost ,
295253 $profile.CurrentUserAllHosts , $profile.CurrentUserCurrentHost
296254 foreach ($file in $profiles ) {
@@ -299,10 +257,6 @@ Function ConvertTo-WPFGrid {
299257 }
300258 }
301259 }
302- if ($InitializationScript ) {
303- Write-Verbose " Loading an initialization scriptblock"
304- $pscmd.AddScript ($InitializationScript ) | Out-Null
305- }
306260
307261 $pscmd.AddScript ($gridScript ) | Out-Null
308262
@@ -347,10 +301,10 @@ Function ConvertTo-WPFGrid {
347301 else {
348302 $cmd = $Scriptblock
349303 }
350-
351- $psboundparameters.cmd = $cmd
352- Write-Verbose " Refresh command: $cmd "
353-
304+ # if ($Refresh) {
305+ $psboundparameters.cmd = $cmd
306+ Write-Verbose " Refresh command: $cmd "
307+ # }
354308 Write-Verbose " Sending PSBoundparameters to runspace"
355309
356310 $psCmd.AddParameters ($PSBoundParameters ) | Out-Null
0 commit comments