nice article in MSDN "Secrets to Building a WPF Application in Windows PowerShell"

Aug 5, 2011 at 2:22 PM


I've found this article in MSDN Mag being quite a very nice introduction to GUI in PowerShell (, code here : Thanks a lot to Doug Finke and all reviewers.

I was a bit confused as the article is recent, from this month issue, but doesn't mention ShowUI, only WPK. I guess it's only due to time for publication as mentionned here ( projects have merged, right?

The code runs without any error after replacing "import-module WPK" by "import-module ShowUI". Really nice!

Of course, as a beginner, there are concepts I don't fully understand. I might raise my hand again, and, if you don't mind I ask over here as I think it's a relevant place!

1. magic of $window

From the "complete application" code, when is $window defined in this statement?

New-Window -Title "PowerShell - WPK Present Value Calculator" -WindowStartupLocation CenterScreen -Width 500 -Height 500 -On_Loaded {
    Do-Calculation $window

I was expecting a new-window -name window before. Are there other default values like this one?

2. magic of data binding

 New-ListView -Row 2 -Column 0 -ColumnSpan 2 -Margin 5 -DataBinding @{ ItemsSource = New-Binding } ...

How is New-Binding wired to the Data Context?

Thanks in advance for any explanation!

Best regards






Aug 5, 2011 at 4:04 PM

Thanks jucg.

That article was based on WPK. Check out my blog post below where I re-wrote it using ShowUI. It is shorter and conforms the ShowUI approach.

Please any other questions this does not answer.



Aug 6, 2011 at 12:43 AM

Hi Doug, 

thanks a lot for your reply. Sorry for missing your latest blog entries. I was in the place, but missed the main info...

I've checked your showUI version of your article. Well, I might be too impatient, or again, I might have passed by the info, but I do have the same questions!

  ListView -Name Results
defines $results, but where does $window come from?
function Show-Results ($window, $results) {
    $parameters = $window | Get-UIValue
    $Results.DataContext = Get-PresentValue @parameters

thanks again

Aug 6, 2011 at 12:55 AM


Yes, $window is an "automatic" variable that ShowUI creates when the main window is created either explicitly or implicitly.

For example, here it is created implicitly:


label "Test" -Show -On_Loaded { $window | Get-Member | Out-Host }


There are others, we need to document them.


In my article I set the DataContext of the $window in the Do-Calculation function. In WPF, all the nested controls will first look to see if their DataContext is set or will continue look up into the parent control till it find one that is set. 

This is why the New-ListView works with the New-Binding and data is displayed.


Aug 7, 2011 at 1:43 PM

Thank you  VERY much for all information. I'll stop bugging you and do a few exercises!

Best regards, jucg