简体   繁体   English

以编程方式更改选定的 TabControl - WPF

[英]Change Selected TabControl Programmatically - WPF

I have a TabControl on my application and am not sure how to swap the tabs when users click on something other than the tab.我的应用程序上有一个 TabControl,当用户单击选项卡以外的其他内容时,我不确定如何交换选项卡。

The initialization is stock:初始化是库存:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

I saw this question ( C# WPF - Open a window and select a TabItem within that window ), which seems to be a Windows Forms app.我看到了这个问题( C# WPF - 打开一个窗口并在该窗口中选择一个 TabItem ),它似乎是一个 Windows 窗体应用程序。

I am not sure how to do the equivalent of Window1 Settings = new Window1();我不知道如何做相当于Window1 Settings = new Window1(); with InitializeComponent();InitializeComponent();

I have tried calling the TabControl by name, but there doesn't seem to be an option to set the active TabItem.我曾尝试按名称调用 TabControl,但似乎没有设置活动 TabItem 的选项。 I tried a few random things like below but couldn't get any response我尝试了一些像下面这样的随机操作,但没有得到任何回应

SoftwareSelection.Items.MoveCurrentToLast();
SoftwareSelection.Items.MoveCurrentTo("Software");

Here is the XAML:这是 XAML:

<TabControl x:Name="SoftwareSelection" SelectionChanged="Software_SelectionChanged"
                                HorizontalAlignment="Left" Height="245" Margin="160,31,0,0" VerticalAlignment="Top"
                                Width="346">
            <TabItem Header="Servers">
                <Grid Background="#FFE5E5E5" Margin="-3,0,-9,0">
                    <TextBlock HorizontalAlignment="Left"
                                           Margin="10"
                                           TextWrapping="Wrap"
                                           Text="Things will go here"
                                           VerticalAlignment="Top"
                                           Width="471" />
                </Grid>
            </TabItem>

            <TabItem Header="Software">
                <Grid Background="#FFE5E5E5" Margin="0,0,1,0">
                    <TextBlock HorizontalAlignment="Left"
                                           Margin="10"
                                           TextWrapping="Wrap"
                                           Text="Stuff will go here"
                                           VerticalAlignment="Top"
                                           Width="471" />

                </Grid>
            </TabItem>
        </TabControl>

只需使用TabControl.SelectedIndexTabControl.SelectedItemTabControl.SelectedValue

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

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