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

Aug 5, 2011 at 2:22 PM

Hi,

I've found this article in MSDN Mag being quite a very nice introduction to GUI in PowerShell (http://msdn.microsoft.com/en-us/magazine/hh288074.aspx, code here : http://archive.msdn.microsoft.com/mag201107PowerShell). 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 (http://www.dougfinke.com/blog/index.php/2011/06/06/wpk-powerboots-powershell-showui) 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

jucg

 

 

 

 

Developer
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.

http://www.dougfinke.com/blog/index.php/2011/07/01/my-msdn-magazine-article-is-outpowershell-wpf-2/

Please any other questions this does not answer.

Thanks

Doug

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

Developer
Aug 6, 2011 at 12:55 AM

jugc

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.

DataContext

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.

Doug

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