简体   繁体   中英

C# WPF - Open a window and select a TabItem within that window

I have the following code to open a window on a button click:

private void LaunchSettings(object sender, RoutedEventArgs e)
    {
        Window1 Settings = new Window1();
        Settings.Show();
    }

But how do I then select a certain TabItem within this newly opened window.

I know you can target a tab programmatically like so:

MyTabControl.SelectedItem = MyTabItem  

Just not sure how to incorporate it into my first snippet of code. So basically, I want this all done in one call.

Window1 should keep a public property of the tab and of the TabBar so you can use it outside and set the tab as selected item for tab bar

This is the answer but you may post Window1 so we can give more accurate code

In Window1.cs add

public var MyTabControl { get; set; }
public var MyTabItem { get; set; }

And in your external code do:

private void LaunchSettings(object sender, RoutedEventArgs e)
{
    Window1 Settings = new Window1();
    Settings.MyTabControl.SelectedItem = Settings.MyTabItem
    Settings.Show();
}

Just remember to set values for the properties

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