简体   繁体   中英

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.

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.

I am not sure how to do the equivalent of Window1 Settings = new Window1(); with InitializeComponent();

I have tried calling the TabControl by name, but there doesn't seem to be an option to set the active 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:

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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