【发布时间】:2018-07-04 19:08:22
【问题描述】:
我有一个不太好用的 ComboBox。 它有 3 个静态选择,用于对一长串连接配置文件进行排序,外加一个自动选择的“选择一个”。
无论我选择什么选项,在我单击另一个选项之前,什么都不会发生。现在这是有趣的部分。无论我接下来选择什么,列表框都会填充我之前的选择。
<ComboBox Name="CBox" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" >
<ComboBoxItem Name="cbSelect" IsSelected="True">Select Profile</ComboBoxItem>
<ComboBoxItem Name="cbRemote">Remote</ComboBoxItem>
<ComboBoxItem Name="cbLab">Lab</ComboBoxItem>
<ComboBoxItem Name="cbTomcats">Tomcats</ComboBoxItem>
</ComboBox>
<ListBox Name="ListBox" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
$syncHash.CBox.Add_SelectionChanged({
If($syncHash.CBox.Text -eq "Select Profile")
{
$syncHash.ListBox.Items.Clear()
}
If($syncHash.CBox.Text -eq "Remote")
{
$syncHash.ListBox.Items.Clear()
$profiles = New-Object System.Collections.ArrayList
$profiles = (Get-ChildItem "C:\Profiles")
foreach ($profile in $profiles)
{
if ($profile -match 'Remote \-')
{
[void]$syncHash.ListBox.Items.Add($profile.BaseName)
}
}
}
If($syncHash.CBox.Text -eq "Lab")
{
$syncHash.ListBox.Items.Clear()
$profiles = New-Object System.Collections.ArrayList
$profiles = (Get-ChildItem "C:\Profiles")
foreach ($profile in $profiles)
{
if ($profile -match 'Lab \-')
{
[void]$syncHash.ListBox.Items.Add($profile.BaseName)
}
}
}
If($syncHash.CBox.SelectedItem -eq "Tomcats")
{
$syncHash.ListBox.Items.Clear()
$profiles = New-Object System.Collections.ArrayList
$profiles = (Get-ChildItem "C:\Profiles")
foreach ($profile in $profiles)
{
if ($profile -match 'Tomcats \-')
{
[void]$syncHash.ListBox.Items.Add($profile.BaseName)
}
}
}
})
使用 Rohin Sidharth 回答的最终解决方案:
Add-Type -Path 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.dll'
$Global:syncHash = [hashtable]::Synchronized(@{})
[xml]$xaml = @"
<Window
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"
Title="ComboBox Test" Height="500" Width="350">
<Grid Width="340" Height="470">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions >
<RowDefinition Height="0.1*" />
<RowDefinition Height="0.9*" />
</Grid.RowDefinitions>
<ComboBox Name="CBox" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" >
<ComboBoxItem Name="cbSelect" IsSelected="True">Select Profile</ComboBoxItem>
<ComboBoxItem Name="cbRemote">Remote</ComboBoxItem>
<ComboBoxItem Name="cbLab">Lab</ComboBoxItem>
<ComboBoxItem Name="cbTomcats">Tomcats</ComboBoxItem>
</ComboBox>
<ListBox Name="ListBox" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[xml]$XAML = $xaml
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | %{
$syncHash.Add($_.Name,$syncHash.Window.FindName($_.Name) )
}
function Get-Profiles
{
$profiles = (Get-ChildItem "C:\Profiles")
$syncHash.ListBox.Items.Clear()
If ($syncHash.CBox.SelectedIndex -eq 1)
{
foreach ($profile in $profiles)
{
if ($profile -match 'Remote \-')
{
$syncHash.ListBox.Items.Add($profile.BaseName)
}
}
}
ElseIf ($syncHash.CBox.SelectedIndex -eq 2)
{
foreach ($profile in $profiles)
{
if ($profile -match 'Lab \-')
{
$syncHash.ListBox.Items.Add($profile.BaseName)
}
}
}
ElseIf ($syncHash.CBox.SelectedIndex -eq 3)
{
foreach ($profile in $profiles)
{
if ($profile -match 'Tomcats \-')
{
$syncHash.ListBox.Items.Add($profile.BaseName)
}
}
}
}
$syncHash.CBox.Add_SelectionChanged({Get-Profiles})
$null = $syncHash.Window.ShowDialog()
$syncHash.Error = $Error
$Error
【问题讨论】:
-
我还没有在 powershell 中使用 WPF,但在 Winforms 中,我认为要在组合框中选择一个项目,您必须设置 SelectedIndex 属性而不是项目的文本。索引 0 将选择下拉列表中的第一项。
-
天才!这很完美。非常感谢。
-
我会把它添加为答案,这样你就可以正确地欣赏我了......哈哈
标签: wpf powershell xaml combobox listbox