WPF and Powershell: XAML(Part 2)

This is the first real example of how WPF could be used by Powershell. It shows how to display a few properties of a collection of objects using XAML and no compiled parts. The XAML in the example script tells WPF how to display the data type we are using(WPF will actually only see the PSObjects that are passed, not the content), and the collection is then handed over to the window.

This is not the only way to do this. It would also be possible to construct the XAML for each object in the collection individually and not need to use a template.

. Library-PresentationInterface

function Get-ProcessDialog {
$Xaml = [Xml](
@"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ps="clr-namespace:System.Management.Automation;assembly=System.Management.Automation"
Name="ThisWindow"
Title="Formated Process List">
<Window.Resources>
<DataTemplate DataType="{x:Type ps:PSObject}">
<Border BorderThickness='0,0,0,1' BorderBrush='Gray' Padding='3'>
<StackPanel>
<TextBlock FontSize='14' FontWeight='Bold' Text="{Binding Properties[ProcessName].Value}" />
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Properties[CPU].Value}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock FontSize='12' Text="CPU: " />
<TextBlock FontSize='12' Text="{Binding Properties[CPU].Value}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Properties[WorkingSet].Value}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock FontSize='12' Text="Working Set: " />
<TextBlock FontSize='12' Text="{Binding Properties[WorkingSet].Value}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Properties[Description].Value}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock FontSize='12' Text="Description: " />
<TextBlock FontSize='12' Text="{Binding Properties[Description].Value}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Properties[Path].Value}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock FontSize='12' Text="Path: " />
<TextBlock FontSize='12' Text="{Binding Properties[Path].Value}" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding ElementName=ThisWindow, Path=Tag}" />
</ScrollViewer>
</Window>
"@
)

$result = Start-PresentationInterface -xaml ($Xaml) -WindowTag (
@($input | select ProcessName, CPU, WorkingSet, Description, Path))
[void](stop-PresentationInterface $result)
}

Get-Process | Get-ProcessDialog

Advertisements

~ by lunaticexperimentalist on November 15, 2007.

One Response to “WPF and Powershell: XAML(Part 2)”

  1. Thanks for posting – interesting stuff.
     
    Can you categorise this entry as "PowerShell" – otherwise it doesn’t get picked up by my reader (RSS Bandit) – thanks!
     
    Chris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: