{"id":6111,"date":"2008-05-23T19:00:14","date_gmt":"2008-05-23T19:00:14","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/05\/23\/wpf-powershell-part-3-handling-events\/"},"modified":"2019-02-18T13:15:54","modified_gmt":"2019-02-18T20:15:54","slug":"wpf-powershell-part-3-handling-events","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/wpf-powershell-part-3-handling-events\/","title":{"rendered":"WPF &#038; PowerShell &#8212; Part 3 (Handling Events)"},"content":{"rendered":"<p>So far, most of the wpf and powershell scripts you have seen have seen just show you something, but don&#8217;t do anything that interactive.<\/p>\n<p>However, In order to make real applications you need to be able to handle events. Luckily, PowerShell can make that pretty easy.<\/p>\n<p>It is possible to cast a script block to an event handler. The script block has two variables: $this (which is the sender), and $_, which contains the event arguments.<\/p>\n<p>Here&#8217;s a simple example of using this to click a button:<\/p>\n<p>$window = New-Object Windows.Window   <br \/>$eventHandler = [Windows.Input.MouseButtonEventHandler]{$this.Close()}    <br \/>$window.Add_MouseDown($eventHandler)    <br \/>$window.Content = &quot;Click Me and I will Go Away&quot;    <br \/>$window.SizeToContent = &quot;WidthAndHeight&quot;    <br \/>$null = $window.ShowDialog()<\/p>\n<p>However, honestly, clicking just one button isn&#8217;t that useful.&#160; Instead, let&#8217;s build something that will help us find commands.&#160; The next example will help you find a command.<\/p>\n<p>$window = New-Object Windows.Window<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/BlogFileStorage\/blogs_msdn\/powershell\/WindowsLiveWriter\/WPFPowerShellPart3HandlingEvents_2417\/SelectCommand_1.jpg\"><img decoding=\"async\" style=\"border-right: 0px;border-top: 0px;border-left: 0px;border-bottom: 0px\" height=\"240\" alt=\"SelectCommand\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/BlogFileStorage\/blogs_msdn\/powershell\/WindowsLiveWriter\/WPFPowerShellPart3HandlingEvents_2417\/SelectCommand_thumb_1.jpg\" width=\"209\" align=\"right\" border=\"0\" \/><\/a>    <br \/>$window.SizeToContent = &quot;WidthAndHeight&quot;    <br \/>$label = New-Object Windows.Controls.Label    <br \/>$label.Content = &quot;Type A command (And watch the list change)&quot;    <br \/>$textBox = New-Object Windows.Controls.TextBox    <br \/>$listBox = New-Object Windows.Controls.Listbox    <br \/>$listBox.Width = 300    <br \/>$listBox.Height = 200    <br \/># When the text changes, use Get-Command to display a shortened list of commands    <br \/>$textBox.add_TextChanged({    <br \/>&#160;&#160;&#160; $listBox.ItemsSource = @(Get-Command &quot;*$($textbox.Text)*&quot; | % { $_.Name })    <br \/>})    <br \/># When the listbox&#8217;s selection changes, set the text to the selection    <br \/>$listBox.add_SelectionChanged({    <br \/>&#160;&#160;&#160; $textBox.Text = $listBox.SelectedItem    <br \/>})    <br \/>$button = New-Object Windows.Controls.Button    <br \/>$button.Content = &quot;Select Command&quot;    <br \/>$button.add_Click({$window.Close()})    <br \/>$stackPanel = New-Object Windows.Controls.StackPanel    <br \/>$stackPanel.Orientation=&quot;Vertical&quot;    <br \/>$children = $label, $textBox, $listbox, $button    <br \/>foreach ($child in $children) { $null = $stackPanel.Children.Add($child) }    <br \/>$window.Content = $stackPanel    <br \/>$null = $window.ShowDialog()    <br \/>$textbox.Text<\/p>\n<p>In the next example, we&#8217;ll show how to use Drag and Drop to populate a listbox.<\/p>\n<p>$window = New-Object Windows.Window<a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/BlogFileStorage\/blogs_msdn\/powershell\/WindowsLiveWriter\/WPFPowerShellPart3HandlingEvents_2417\/DragAndDrop.jpg\"><img decoding=\"async\" style=\"border-right: 0px;border-top: 0px;border-left: 0px;border-bottom: 0px\" height=\"153\" alt=\"DragAndDrop\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/BlogFileStorage\/blogs_msdn\/powershell\/WindowsLiveWriter\/WPFPowerShellPart3HandlingEvents_2417\/DragAndDrop_thumb.jpg\" width=\"324\" align=\"right\" border=\"0\" \/><\/a>     <br \/>$window.SizeToContent = &quot;WidthAndHeight&quot;    <br \/>$label = New-Object Windows.Controls.Label    <br \/>$window.Title = $label.Content = &quot;Drag Scipts Here, DoubleClick to Run&quot;    <br \/>$listBox = New-Object Windows.Controls.Listbox    <br \/>$listBox.Height = 200    <br \/>$listBox.AllowDrop = $true    <br \/>$listBox.add_MouseDoubleClick({Invoke-Expression &quot;$($listbox.SelectedItem)&quot; -ea SilentlyContinue })&#160;&#160;&#160; <br \/>$displayedFiles = @()    <br \/>$listBox.add_Drop({    <br \/>&#160;&#160;&#160; $files = $_.Data.GetFileDropList()    <br \/>&#160;&#160;&#160; foreach ($file in $files) {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160; if ($file -is [IO.FileInfo]) {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $displayedFiles = $file    <br \/>&#160;&#160;&#160;&#160;&#160;&#160; } else {    <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $displayedFiles += dir $file -recurse | ? { $_ -is [IO.FileInfo]} | % { $_.FullName }     <br \/>&#160;&#160;&#160;&#160;&#160;&#160; }    <br \/>&#160;&#160;&#160; }    <br \/>&#160;&#160;&#160; $listBox.ItemsSource = $displayedFiles | sort    <br \/>})    <br \/>$runButton = New-Object Windows.Controls.Button    <br \/>$runButton.Content = &quot;Run&quot;    <br \/>$runButton.add_Click({Invoke-Expression &quot;$($listbox.SelectedItem)&quot; -ea SilentlyContinue })    <br \/>$clearButton = New-Object Windows.Controls.Button    <br \/>$clearButton.Content = &quot;Clear&quot;    <br \/>$clearButton.add_Click({$listBox.ItemsSource = @()})    <br \/>$stackPanel = New-Object Windows.Controls.StackPanel    <br \/>$stackPanel.Orientation=&quot;Vertical&quot;    <br \/>$children = $label, $listbox, $runButton, $clearButton    <br \/>foreach ($child in $children) { $null = $stackPanel.Children.Add($child) }    <br \/>$window.Content = $stackPanel    <br \/>$null = $window.ShowDialog() <\/p>\n<p>&#160;<\/p>\n<p>Tomorrow, we&#8217;ll make both of these relatively short (30-40 line scripts) a lot shorter and start introducing you to XAML, the cool XML format (with a lot of designer tools) you can use to write user interfaces with WPF.<\/p>\n<p>Hope this helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far, most of the wpf and powershell scripts you have seen have seen just show you something, but don&#8217;t do anything that interactive. However, In order to make real applications you need to be able to handle events. Luckily, PowerShell can make that pretty easy. It is possible to cast a script block to [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[168,248,360],"class_list":["post-6111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-get-command","tag-powershell","tag-wpf"],"acf":[],"blog_post_summary":"<p>So far, most of the wpf and powershell scripts you have seen have seen just show you something, but don&#8217;t do anything that interactive. However, In order to make real applications you need to be able to handle events. Luckily, PowerShell can make that pretty easy. It is possible to cast a script block to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6111"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6111\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}