如何在组合框中动态更新可用的东西

How to update available things dynamically in combobox

我做了一个 WPF XAML 应用程序,它在后台使用 PowerShell 来做事。这是我写的一段代码。

function Add-ControlVariables {

New-Variable -Name 'Login' -Value $window.FindName('Login') -Scope 1 -Force 
New-Variable -Name 'AvailableSubscriptions' -Value $window.FindName('AvailableSubscriptions') -Scope 1 -Force   
New-Variable -Name 'AvailableResourceGroups' -Value $window.FindName('AvailableResourceGroups') -Scope 1 -Force 
New-Variable -Name 'AvailablLogicApps' -Value $window.FindName('AvailablLogicApps') -Scope 1 -Force
}

function Load-Xaml {
    [xml]$xaml = Get-Content -Path $PSScriptRoot\Azure.xaml
    $manager = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xaml.NameTable
    $manager.AddNamespace("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    $xaml.SelectNodes("//*[@x:Name='Login']", $manager)[0].RemoveAttribute('Click')
    $xaml.SelectNodes("//*[@x:Name='AvailableSubscriptions']", $manager)[0].RemoveAttribute('SelectionChanged')
    $xaml.SelectNodes("//*[@x:Name='AvailableResourceGroups']", $manager)[0].RemoveAttribute('SelectionChanged')
    $xaml.SelectNodes("//*[@x:Name='AvailablLogicApps']", $manager)[0].RemoveAttribute('SelectionChanged')
    $xamlReader = New-Object System.Xml.XmlNodeReader $xaml
    [Windows.Markup.XamlReader]::Load($xamlReader)
}

function Set-EventHandlers {

    $Login.add_Click({
        param([System.Object]$sender,[System.Windows.RoutedEventArgs]$e)
        Login($sender,$e)
        Connect-AzureRmAccount
        $AvailableSubscriptions.ItemsSource = Get-AzureRmSubscription | Select-Object -ExpandProperty Name
    })
    $AvailableSubscriptions.add_SelectionChanged({
        param([System.Object]$sender,[System.Windows.Controls.SelectionChangedEventArgs]$e)
        AvailableSubscriptions_SelectionChanged($sender,$e)
    })
    $AvailableResourceGroups.add_SelectionChanged({
        param([System.Object]$sender,[System.Windows.Controls.SelectionChangedEventArgs]$e)
        $resourceGroupName = $sender.SelectedItem
        $AvailablLogicApps.ItemsSource = Get-AzureRmResource -ResourceType "Microsoft.Logic/workflows" -ResourceGroupName $resourceGroupName | Select-Object -ExpandProperty Name
    })
    $AvailablLogicApps.add_SelectionChanged({
        param([System.Object]$sender,[System.Windows.Controls.SelectionChangedEventArgs]$e)
    })

}
$window = Load-Xaml
Add-ControlVariables
Set-EventHandlers

function AvailableSubscriptions_SelectionChanged
{
    param($sender, $e)
    $subscription = $sender.SelectedItem
    Select-AzureRmSubscription -SubscriptionName $subscription
    $AvailableResourceGroups.ItemsSource = Get-AzureRmResourceGroup | Select-Object -ExpandProperty ResourceGroupName
}

$window.ShowDialog()

这是 UI。我希望订阅被列为单一的东西。它将订阅名称显示为单独的字符。

https://imgur.com/a/BY424pz

ItemsSource 分配更改为

$AvailableResourceGroups.ItemsSource =[Collections.Generic.List[String]](Get-AzureRmResourceGroup | Select-Object -ExpandProperty ResourceGroupName)