title: Adding handler for TextBoxBase.TextChanged on ComboBox in PowerShell
body: <p>I'm writing a PowerShell script that uses WPF to create a small GUI. I want to add detection for when text in a ComboBox is changed, using the TextBoxBase.TextChanged event.</p>
  
  <p>Here are a few things I've tried:</p>
  
  <pre><code>#Build the GUI; the next line has to NOT be indented
  [xml]$xaml =
  @"
  &lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Name="Window" Title="Export Readable Prefs" WindowStartupLocation="CenterScreen" 
      Width="600" Height="200" ShowInTaskbar="True"&gt;
      &lt;DockPanel Margin="5"&gt;
          &lt;ComboBox Grid.Row="0" x:Name="ComboNewTemplate" IsEditable="true" /&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  "@
  
  $reader = (New-Object System.Xml.XmlNodeReader $xaml)
  $Window = [Windows.Markup.XamlReader]::Load($reader)
  
  $comboNewTemplate  = $Window.FindName("ComboNewTemplate")
  
  # this works
  $comboNewTemplate.Add_SelectionChanged({Write-Host $_.AddedItems[0]})
  
  # this doesn't
  $comboNewTemplate.Add_TextBoxBase_TextChanged({Write-Host "text changed"})
  
  # this doesn't, presumably because it hasn't been created yet
  $textBox = $comboNewTemplate.Template.FindName("PART_EditableTextBox", $comboNewTemplate)
  $textBox.Add_TextChanged({Write-Host "Text changed"})
  
  # this doesn't; not even sure if this makes sense, it's out of my depth
  function qwe(){"text changed"}
  $func = qwe;
  [System.Runtime.InteropServices.Marshal]::StructureToPtr($func, $intptr, $true)
  $comboNewTemplate.AddHandler([System.Windows.Controls.Primitives.TextBoxBase]::TextChangedEvent, `
                               [System.Windows.RoutedEventHandler]::new($o1, $intptr))
  
  $Window.ShowDialog()
  </code></pre>