我有一个不太合适的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
答案 0 :(得分:0)
我还没有在PowerShell中使用WPF,但在Winforms中,我想在组合框中选择一个项目,你必须设置SelectedIndex
属性而不是项目的文本。索引为0将选择下拉列表中的第一项