Wpf 在PowerShell中的ComboBox上添加TextBoxBase.TextChanged的处理程序
我正在编写一个PowerShell脚本,它使用WPF创建一个小型GUI。我想使用TextBoxBase.TextChanged事件添加组合框中文本更改时的检测 以下是我尝试过的几件事:Wpf 在PowerShell中的ComboBox上添加TextBoxBase.TextChanged的处理程序,wpf,powershell,Wpf,Powershell,我正在编写一个PowerShell脚本,它使用WPF创建一个小型GUI。我想使用TextBoxBase.TextChanged事件添加组合框中文本更改时的检测 以下是我尝试过的几件事: #Build the GUI; the next line has to NOT be indented [xml]$xaml = @" <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml
#Build the GUI; the next line has to NOT be indented
[xml]$xaml =
@"
<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">
<DockPanel Margin="5">
<ComboBox Grid.Row="0" x:Name="ComboNewTemplate" IsEditable="true" />
</DockPanel>
</Window>
"@
$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()
#构建GUI;下一行不必缩进
[xml]$xaml=
@"
"@
$reader=(新对象System.Xml.XmlNodeReader$xaml)
$Window=[Windows.Markup.XamlReader]::加载($reader)
$comboNewTemplate=$Window.FindName(“comboNewTemplate”)
#这很有效
$comboNewTemplate.Add_SelectionChanged({Write Host$_.AddedItems[0]})
#这并不重要
$comboNewTemplate.Add_TextBoxBase_TextChanged({Write Host“text changed”})
#这没有,大概是因为它还没有被创建
$textBox=$comboNewTemplate.Template.FindName(“PART_EditableTextBox”,$comboNewTemplate)
$textBox.Add_TextChanged({Write Host“Text changed”})
#这并不重要;我甚至不确定这是否有意义,这超出了我的深度
函数qwe(){“文本已更改”}
$func=qwe;
[System.Runtime.InteropServices.Marshal]::StructureToPtr($func,$intptr,$true)
$comboNewTemplate.AddHandler([System.Windows.Controls.Primitives.TextBoxBase]::TextChangedEvent`
[System.Windows.RoutedEventHandler]::新($o1,$intptr))
$Window.ShowDialog()
您可以这样做:
$comboNewTemplate.AddHandler(
[System.Windows.Controls.Primitives.TextBoxBase]::TextChangedEvent,
[System.Windows.RoutedEventHandler]{ Write-Host "text changed" })