在阅读FoxDeploy's tutorial关于使用WPF和XAML构建PowerShell GUI之后,我使用制表符构建了自己的应用程序。但是,我正在努力让我的Tabs按照我想要的方式行事。
XAML文件
<Window x:Class="Tabs.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:Tabs"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Width="500">
<TabControl x:Name="TabControl">
<TabItem x:Name="Tab1" Header="First" Width="70">
<Grid x:Name="Tab1Grid" Background="#FFD4D4D4">
<Label Content="Tab 1" VerticalAlignment="Top" FontFamily="Calibri" FontSize="18" HorizontalContentAlignment="Center" Background="#FF5D5656" Foreground="White"/>
<Button x:Name="btnTab2" Content="Go to tab 2 " HorizontalAlignment="Left" Margin="405,256,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</TabItem>
<TabItem x:Name="Tab2" Header="Second" Width="70">
<Grid x:Name="Tab2Grid" Background="#FFD4D4D4">
<Label Content="This is the second tab" VerticalAlignment="Top" FontFamily="Calibri" FontSize="18" HorizontalContentAlignment="Center" Background="#FF5D5656" Foreground="White"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
PS1文件
$inputXML = Get-Content -Path path_to_MainTabbedWindow.xaml
$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
try { $Form=[Windows.Markup.XamlReader]::Load( $reader ) }
catch { Write-Host "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .net is installed." }
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name)}
$WPFbtnTab2.Add_Click({
# change focus to Tab 2
# populate a listview based on input on tab 1
})
$Form.ShowDialog() | out-null
因此,当我点击“转到标签2”时,我想显示标签 - 与点击上方相同 - 但我还想根据在标签1上输入的某些内容填充标签2上的列表视图。
Tabs可能与我上面写的相同,或者我是否需要为标签使用不同的Form对象并根据需要隐藏/显示(同样的事情,右)?
提前感谢任何建议或帮助。
答案 0 :(得分:0)
道歉,看起来好像脱离了kbd,Mathias的评论点击了右脑细胞。
这就是诀窍:
$WPFTabControl.Items[0].IsSelected = $false
$WPFTabControl.Items[1].IsSelected = $true