Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

WPF from PowerShell, the making of the WPF WMI Explorer Part3

In PowerScripting Podcast - Episode 27 - Darren Mar-Elia talks about Group Policy , my WPF work was mentioned, and as there is a real WPF from PowerShell explosion, as noted last post : More PoSHy WPF from PowerShell coverage ,

Some remarks on that part, glad you liked the post,  and thanks for the podcast entry but I was targeting  "non-devs" ofcourse,

and we are not talking SilverLight here but WPF (Windows Presentation Foundation)  ;-)

Some small mistakes but I think we got Hal more updated on WPF on IRC

I also liked the interview with Darren, another great episode in the PowerScripting  PodCast series and keep up the good work !!

But back to the WPF :

James goes very fast on the Windows PowerShell Team Blog , already at WPF & PowerShell - Part 6 (Running Functions in the Background) , and Jaykul also seems up for more WPF coverage.

Still I think a part3 in the series featuring a very simple, but powerful XAML template for use in PowerShell, might help So I made another post.

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="400" Width="500">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100*" />
            <ColumnDefinition Width="180*" />
        </Grid.ColumnDefinitions>
        <GridSplitter Grid.Column="1" HorizontalAlignment="Left" Name="gridSplitter1" Width="10" />
        <ListBox  Name="listBox" />
        <RichTextBox Grid.Column="1"  Name="richTextBox1" />
    </Grid>
</Window>

I initially created this in VS2008, but the XAML editor GUI in VS is not really handy and it is hard to size, but a handy way (a tip I got from Jaykul, while I was working on the  PowerShell WMI Explorer WPF Edition first beta) , a handy way is just remove most of the properties VS2008 does add and clear the XAML code by hand.

When you remove the size the control will take all available space (like DOCK does with form controls) , and as we added a splitter control we can resize on the fly, just what we want.

what is left is the code above that I did save as TextViewer.xaml

And I made the following PowerShell function  :

Add-Type -AssemblyName presentationframework 

function view-TextFiles ($dir = '.') {
 
  # Create WPF Form from XAML file and bind Controls to variables
    
  $path = Resolve-Path $dir
  $Form=[Windows.Markup.XamlReader]::Load([IO.File]::OpenText('c:\powershell\textviewer.xaml').basestream) 
  $ListBox = $form.FindName('listBox')
  $RichTextBox = $form.FindName('richTextBox')
  
  # Fill ListBox

  ls *.txt |% {
    $li = new-Object Windows.Controls.ListBoxItem
    $li.Content = $_.name
    $li.Tag= $_.fullname
    $listbox.Items.Add($LI)
  }
  
  # Add Handler to Read File
  
  $ListBox.add_MouseDoubleClick({
    $RichTextBox.SelectAll()
    $RichTextBox.Selection.Text = gc $this.Selecteditem.Tag
  })

  # Invoke Form 
  
  $Form.ShowDialog() | out-null
}

Four simple steps

Create Form

the same code as in former examples in this series only reading the XAML from file using a FileStream,

Fill Listbox

a simple LS does get all textfile and create listviewitems for it

Add Handler,

does a get-content on the fullname of the Textfile we saved as a TAG

Invoke the form :

image

And we have build a fileViewer, OK, we better could take a scriptblock instead of hardcoding the fill and this is example is kept very simple, but it is easy adapt and to make this more flexible,

You can see that we can easy pick other controls to use (think TreeView / Listview ), and these kind of simple forms, kept in a library might come handy often.

I hope this simpler sample helped, grasping all that other cool WPF stuff James and Jakul are doing in there series.

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, May 27, 2008 5:57 PM by MoW
Filed under: ,

Comments

# Interesting Finds: May 28, 2008

Wednesday, May 28, 2008 10:15 AM by Jason Haley
Anonymous comments are disabled