使用 Add_SelectedValueChanged() 从 ComboBox 赋值

Assigning value from ComboBox with Add_SelectedValueChanged()

我正在尝试从组合框中检索文本。寻找答案,似乎我需要使用 Add_SelectedValueChanged 方法,但我没有在我的 $form 变量上看到该方法。我还检查了 $WPFproductTxt 变量,但它也不存在。我错过了什么?

我的代码片段(忽略表单中的空格,我把字段拿出来测试):

$rawXAML = @"
<Window x:Class="WpfApp8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp8"
        mc:Ignorable="d"
        Title="Test" Height="650" Width="425">
    <Grid Margin="0,0,0,1">
        <TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="10,10,0,0" Text="Test form" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="16" FontWeight="Bold"/>
        <Label x:Name="custNameLabel" Content="Customer Name*" Margin="10,43,262,0" VerticalAlignment="Top" />
        <TextBox x:Name="custNameTxt" Height="26" Margin="158,43,10,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="" />
        <StackPanel Margin="10,263,10,269">
            <Label FontWeight="DemiBold" BorderThickness="2" BorderBrush="Gray" HorizontalContentAlignment="Center">Hardware</Label>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>
                <RadioButton x:Name="sharedMark" Content="Shared" IsChecked="False" HorizontalAlignment="Center"/>
                <RadioButton x:Name="dedicatedMark" Content="Dedicated" IsChecked="False" Grid.Row="1" HorizontalAlignment="Center"/>
                <ComboBox x:Name="productTxt" Margin="0,5,0,5" Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" ToolTip="Hardware type." HorizontalAlignment="Center" Width="182">
                    <ComboBoxItem Content="Gen 3 SFF" IsSelected="False"/>
                    <ComboBoxItem Content="Gen 3 Rack" IsSelected="False"/>
                    <ComboBoxItem Content="Gen 4 Rack" IsSelected="False"/>
                    <ComboBoxItem Content="Virtual Machine" IsSelected="False"/>
                </ComboBox>
            </Grid>
        </StackPanel>
        <Button x:Name="okButton" Content="OK" HorizontalAlignment="Left" Margin="13,578,0,0" VerticalAlignment="Top" Width="50" />
        <Button x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="68,578,0,0" VerticalAlignment="Top" Width="50"/>
    </Grid>
</Window>
"@

$rawXAML = $rawXAML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'

[void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
[xml]$XAML = $rawXAML

# Read XAML.
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$form = [Windows.Markup.XamlReader]::Load($reader)
$xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) }
$WPFokButton.Add_Click( {
        $form.Close()
    })
$WPFcancelButton.Add_Click( { Set-Variable -Name UserCancelled -Value $true -Scope Global })
$null = $form.ShowDialog()

我也不知道,所以我将其输入控制台以获取可用于 ComboBox 的事件:

$WPFproductTxt | Get-Member -MemberType Event

这列出了一个 SelectionChanged 事件。因此我能够写:

$WPFproductTxt.Add_SelectionChanged({
    [Windows.MessageBox]::Show( $WPFproductTxt.SelectedValue )
})

当我 select 一个不同的 ComboBox 项目时,此事件显示一个消息框。