Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

2009-11-13

WPF Button with click event using PowerShell

A WPF Button with a click event can basically be defined either programmatic or with XAML. The two examples below gives the same look and feel in both ways.

First the programmatic way:
function Show-pgmButton {
    $Window = New-Object System.Windows.Window
    $Window.Title="SQLAdmin"
    $Window.Height="250"
    $Window.Width="400"
   
    $Button = New-Object System.Windows.Controls.Button
    $Button.Height = 23
    $Button.Width = 100
    $Button.Margin = '12,12,0,0'
    $Button.VerticalAlignment = 'Top'
    $Button.HorizontalAlignment = 'Left'
    $Button.Content = "Don't click me!"
    $Button.add_Click({
        $Button.Content = 'Arrrgh!!!'
        Start-Process '\\Titanium\Musik'
    })
    $Grid = New-Object System.Windows.Controls.Grid
    $Grid.Children.Add( $Button )
   
    $Window.Content = $Grid
    [void]$Window.ShowDialog()
}

Then the XAML way:
function Show-xamlButton {
[xml]$XAML = @'
<Window
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="SQLAdmin" Height="250" Width="400">
   <Grid>
      <Button Height="22" Width="100" Name="myButton" Content="Don't click me!" VerticalAlignment="Top" HorizontalAlignment="Left" />
   </Grid>
</Window>
'@

   $Reader = New-Object System.Xml.XmlNodeReader $XAML
   $Window = [System.Windows.Markup.XamlReader]::Load( $Reader )

   $myButton = $Window.FindName('myButton')
   $myButton.Add_Click({
      $myButton.Content = 'Arrrgh!!!'
      Start-Process '\\Titanium\musik'
   })

   [void]$Window.ShowDialog()
}

2009-01-17

WPF Menu using XAML and PowerShell

Success - Inspired by a blog entry from The PowerShell GUY (link) I managed to get a solution on the last WPF example, but this time using XAML:

Add-Type –assemblyName PresentationFramework

[xml]$XAML = @'
<window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SQLAdmin" Height="250" Width="400">
<Grid>
<Menu Height="22" Name="MainMenu" VerticalAlignment="Top" HorizontalAlignment="Stretch">
<MenuItem Header="_Systems" Name="SystemsMenuItem">
<MenuItem Header="_Tools">
<MenuItem Header="Management Studio" Name="MgmtStudMenuItem">
</menuitem>
</menu>
<Canvas Name="ContentCanvas" Margin="0,22,0,0">
</grid>
</window>
'@
$Reader = New-Object System.Xml.XmlNodeReader $XAML
$Form = [Windows.Markup.XamlReader]::Load( $Reader )

$Label = New-Object Windows.Controls.Label
$Label.Content = "This initial laben contain several lines`nof text."

$ContentCanvas = $Form.FindName("ContentCanvas")
$ContentCanvas.Children.Add($Label)

$SystemsMenuItem = $Form.FindName("SystemsMenuItem")
$SystemsMenuItem.add_click({ $Label.Content = 'SQLAdmin' })

$MgmtStudMenuItem = $Form.FindName("MgmtStudMenuItem")
$MgmtStudMenuItem.Add_Click({ $Label.Content = 'Starting SQL Server Management Studio...' })

$Form.ShowDialog() | out-null


The result is similar to that of the former WPF MenuItem example.

2009-01-13

WPF Menu using PowerShell

Usually I use HTA for a quick application, but with PowerShell V2 it's possible to use Windows Presentation Foundation (WPF) with a lot of nice features I don't have to code from the bottom.

This little script is a investigation into the MenuItem class and click events:

$Window = New-Object Windows.Window
$Window.Title="SQLAdmin"
$Window.Height="250"
$Window.Width="400"

$SystemMenuItem = New-Object Windows.Controls.MenuItem
$SystemMenuItem.Header = "_Systems"
$SystemMenuItem.add_Click({ $Label.Content = 'SQLAdmin' })

$ManStudToolMenuItem = New-Object Windows.Controls.MenuItem
$ManStudToolMenuItem.Header = "_Management Studio"
$ManStudToolMenuItem.add_Click({ $Label.Content = 'Starting SQL Server Management Studio...' })

$ToolMenuItem = New-Object Windows.Controls.MenuItem
$ToolMenuItem.Header = "_Tools"
$ToolMenuItem.Items.Add($ManStudToolMenuItem)

$Menu = New-Object Windows.Controls.Menu
$Menu.Height="22"
$Menu.VerticalAlignment="Top"
$Menu.HorizontalAlignment="Stretch"
$Menu.Items.Add($SystemMenuItem)
$Menu.Items.Add($ToolMenuItem)

$Label = New-Object Windows.Controls.Label
$Label.Content = "This initial laben contain several lines`nof text."

$ContentCanvas = New-Object Windows.Controls.Canvas
$ContentCanvas.Margin="0,22,0,0"
$ContentCanvas.Children.Add($Label)

$Grid = New-Object Windows.Controls.Grid
$Grid.Children.Add($Menu)
$Grid.Children.Add($ContentCanvas)

$Window.Content = $Grid

[void]$Window.ShowDialog()


When the script starts, this window is shown:



Clicking the menu item "Systems" changes the contents of the white area:


The menu item "Tools" has a sub-item "Management Studio":

Clicking the menu item "Management Studio" changes the contents of the white area (again...):

Well - not exactly Rocket Science, but I found out how to use WPF MenuItem :-)
Still the formatting is done programatically. I would like to know how to use XAML for formatting and PowerShell for functionality.
References
MSDN Library > Windows Presentation Foundation
Windows PowerShell Blog : WPF & PowerShell -- Part 3 (Handling Events)
Huddled Masses : WPF From PowerShell - Select-Grid
Charles Petzold : Applications = Code + Markup (ISBN-13: 978-0-7356-1957-9)