WPK – 2: Some thoughts, and my first really small databound app

My second article on using WPF from PowerShell. You can download WPK as part of the Windows 7 Resource Kit PowerShell Pack. When you can navigate to the folder <My Documents>\WindowsPowerShell\Modules you see the modules that are installed. The folder WPK contains the WPK module.

On the Modules level a few documents are installed:

  • Readme1st.txt – information on installing, using, uninstalling the PowerShellPack
  • About the Windows 7 Resource Kit PowerShell Pack.docx – an overview of the available modules
  • Writing User Interfaces with WPK.docx - the first place to get started when working with WPK

Especially the Readme1st.txt contains two interesting pieces of information:

  1. The file starts with the following text:

    Readme for the Windows 7 Resource Kit PowerShell Pack

                             by James Brundage

                 Copyright (c) 2009 by Microsoft Corporation
                  Portions copyright (c) 2009 James Brundage
                             All Rights Reserved


    I thought James Brundage is an employee of Microsoft, why does he own portions of the copyright?

  2. The disclaimer contains the following text:

    The Windows 7 Resource Kit PowerShell Pack included on the companion CD is
    unsupported by Microsoft and is provided to you as-is, with no warranty or
    guarantee concerning its functionality. For the latest news and usage tips
    concerning this PowerShell Pack, see the Windows PowerShell Team Blog at
    http://blogs.msdn.com/powershell/.

    So no support from Microsoft’s side. I wonder how issues will be resolved and new releases will be published.

Ok, and now on to some programming. In the document Writing User Interfaces with WPK.docx we find a nice example of a process viewer that updates the list of processes every 15 seconds.

A simple process viewer
  1. New-ListView -Width 350 -Height 350 -DataBinding @{
  2.    ItemsSource = New-Binding -IsAsync -UpdateSourceTrigger PropertyChanged -Path Output
  3. } -View {
  4.    New-GridView -AllowsColumnReorder -Columns {
  5.        New-GridViewColumn "Name"
  6.        New-GridViewColumn "Id"
  7.    }
  8. } -DataContext {
  9.    Get-PowerShellDataSource -Script {
  10.        Get-Process | ForEach-Object { $_ ; Start-Sleep -Milliseconds 25 }
  11.    }
  12. } -On_Loaded {
  13.    Register-PowerShellCommand -Run -In "0:0:15" -ScriptBlock {
  14.        $window.Content.DataContext.Script = $window.Content.DataContext.Script
  15.    }
  16. } -asjob

See the document for a great explanation on how it works.

When looking at this example I had a few questions:

  1. What if I don’t want to do a timed update, but just bind to some existing data?
  2. All examples do the data collection in the Get-PowerShellDataSource script block, is it possible to have the data already somewhere in a variable?
  3. Can I skip the binding stuff, I know its really powerful, but I want to start simple?
  4. Retrieve data in a background job is really cool, but what if we just want to load data and go?

My first simple try after a lot of testing and tweaking is the following:

 

Some names and ages
  1. New-ListView -Show -DataBinding @{
  2.    ItemsSource = New-Binding -Path Output
  3. } -View {
  4.    New-GridView -Columns {
  5.        New-GridViewColumn "Name"
  6.        New-GridViewColumn "Age"
  7.        }
  8. } -DataContext {
  9.    Get-PowerShellDataSource -Script {
  10.        $list = @()
  11.        $list += New-Object Object |
  12.            Add-Member NoteProperty Name "Serge" -passthru |
  13.            Add-member NoteProperty Age "43" -passthru
  14.        $list += New-Object Object |
  15.            Add-Member NoteProperty Name "Dinah" -passthru |
  16.            Add-member NoteProperty Age "42" -passthru
  17.        $list += New-Object Object |
  18.            Add-Member NoteProperty Name "Scott" -passthru |
  19.            Add-member NoteProperty Age "8" -passthru
  20.        $list += New-Object Object |
  21.            Add-Member NoteProperty Name "Dean" -passthru |
  22.            Add-member NoteProperty Age "4" -passthru
  23.        $list += New-Object Object |
  24.            Add-Member NoteProperty Name "Tahne" -passthru |
  25.            Add-member NoteProperty Age "1" -passthru
  26.        $list
  27.    }
  28. }

 

I still use binding to the output, and in the datacontext script block I write the elements to bind to to the output. I still don’t bind to pre-calculated data in a variable.

After a lot more testing I came to the following code:

Names and ages from variable
  1. $list = @()
  2. $list += New-Object Object |
  3.    Add-Member NoteProperty Name "Serge" -passthru |
  4.    Add-member NoteProperty Age "43" -passthru
  5. $list += New-Object Object |
  6.    Add-Member NoteProperty Name "Dinah" -passthru |
  7.    Add-member NoteProperty Age "42" -passthru
  8. $list += New-Object Object |
  9.    Add-Member NoteProperty Name "Scott" -passthru |
  10.    Add-member NoteProperty Age "8" -passthru
  11. $list += New-Object Object |
  12.    Add-Member NoteProperty Name "Dean" -passthru |
  13.    Add-member NoteProperty Age "4" -passthru
  14. $list += New-Object Object |
  15.    Add-Member NoteProperty Name "Tahne" -passthru |
  16.    Add-member NoteProperty Age "1" -passthru
  17.  
  18. New-ListView -Name mylistview -Show -View {
  19.    New-GridView -Columns {
  20.        New-GridViewColumn "Name"
  21.        New-GridViewColumn "Age"
  22.    }
  23. } -On_Loaded {
  24.    $mylistview = $window | Get-ChildControl mylistview
  25.    $mylistview.ItemsSource = $list
  26. }

 

In the above code I create a variable with a list of objects with two properties, Name and Age, and bind the ItemsSource property of the ListView to this variable.

I bind the data in the Loaded event, the complete control tree is in place when this event fires.

I have named the ListView control ‘mylistview’, and with the  code in line 24 I can find the control by name. The $window variable points to the implicitly created Window control surrounding the ListView, and is always available.

Note that if we add the –AsJob parameter to the New-ListView command, the creation and binding is done in a background job on another thread, and the $list variable is not visible, not even if it is defined as a global variable (as $global:list)

Diving into this simplification kept me busy for a while and gave me as a WPF nono some insights in how things are working in WPF when doing this from PowerShell.

One question I still have in this simple example: is there an easier way to fill the $list variable so its still useful for databinding.I tried $list = @{ Name="Serge"; Age="43" }, @{ Name="Dinah"; Age="42" } but that does not work:-(

Let me know if this is of any help to you.

Happy coding!

14 Comments

  • Thanks for the post. Helpful example. BTW, there are a couple Powershell MVPs doing some amazing work with MindTouch (www.MindTouch.com) and Powershell. Check it out.

  • this looks somewhat shorter:
    $list = @()
    $list += New-Object PSObject -Property @{Name="Serge";Age="43"}
    $list += New-Object PSObject -Property @{Name="Dinah";Age="42"}
    $list += New-Object PSObject -Property @{Name="Scott";Age="8"}
    $list += New-Object PSObject -Property @{Name="Dean";Age="4"}
    $list += New-Object PSObject -Property @{Name="Tahne";Age="1"}

  • So-so. Something was not impressed.

  • My coder is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses.

    But he's tryiong none the less. I've been using WordPress on a number of websites for
    about a year and am anxious about switching to another platform.
    I have heard excellent things about blogengine.net. Is there a way I can import all
    my wordpress content into it? Any kind of help would be really appreciated!

  • It's an remarkable article designed for all the online viewers; they will take advantage from it I am sure.

  • If some one needs to be updated with hottest technologies then he
    must be go to see this web page and be up to date every day.

  • I am truly thankful to the owner of this site who has shared
    this impressive article at at this time.

  • At this time it appears like BlogEngine is the top blogging
    platform out there right now. (from what I've read) Is that what you are using on your blog?

  • I do not know whether it's just me or if perhaps everyone else encountering issues with your blog. It appears as though some of the text on your posts are running off the screen. Can somebody else please comment and let me know if this is happening to them too? This might be a problem with my internet browser because I've had this happen before.

    Many thanks

  • It's great that you are getting ideas from this paragraph as well as from our dialogue made at this place.

  • If some one needs expert view concerning running a
    blog after that i propose him/her to pay a visit this
    blog, Keep up the nice work.

  • Hey there! This is my first visit to your blog! We are a group of volunteers and starting a new initiative
    in a community in the same niche. Your blog provided us valuable information to work on.

    You have done a marvellous job!

  • Heya! I realize this is sort of off-topic but I needed to
    ask. Does managing a well-established website like yours take a large
    amount of work? I'm completely new to running a blog but I do write in my journal everyday. I'd like to start a blog so I will be able to share my personal
    experience and views online. Please let me know if you have
    any kind of recommendations or tips for brand new aspiring blog owners.
    Thankyou!

  • I love it when people come together and share opinions.
    Great website, keep it up!

Comments have been disabled for this content.