简体   繁体   English

Powershell + WPF XAML + Tabs-更改选项卡

[英]Powershell + wpf xaml + Tabs - change tab

After reading FoxDeploy's tutorial on building a PowerShell GUI with WPF and XAML, I built my own app with tabs. 阅读了FoxDeploy关于使用WPF和XAML构建PowerShell GUI 的教程之后,我使用选项卡构建了自己的应用程序。 However, I am struggling to get my Tabs to behave the way I want. 但是,我正在努力让我的Tabs发挥我想要的方式。

XAML file 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 file 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

Picture of PS GUI PS GUI图片

So, when I click "Go to tab 2" I want to show the tab - same as clicking above - but I also want to populate a list view on Tab 2 based on some content entered on Tab 1. 因此,当我单击“转到选项卡2”时,我想显示该选项卡-与单击上方相同-但我也想根据在选项卡1上输入的某些内容来填充选项卡2上的列表视图。

Is it possible with Tabs in the same way I have written above or do I need to use different Form objects for the tabs and hide/show as required (same thing, right)? 选项卡是否可能以我上面编写的相同方式出现?或者我是否需要为选项卡使用不同的Form对象并根据需要隐藏/显示(相同的东西,对)?

Thanks in advance for any suggestions or help. 在此先感谢您的任何建议或帮助。

Apologies, looks like a good break away from the kbd and a comment from Mathias clicked the right brain cells in place. 道歉,似乎是与kbd的良好突破,Mathias的评论单击了正确的右脑细胞。

This did the trick: 这达到了目的:

$WPFTabControl.Items[0].IsSelected = $false
$WPFTabControl.Items[1].IsSelected = $true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM