// Create the TabControl TabControl tabControl = new TabControl(); // Create the first tab and add a Label control TabItem tab1 = new TabItem(); tab1.Header = "Tab 1"; Label label1 = new Label(); label1.Content = "This is Tab 1"; tab1.Content = label1; tabControl.Items.Add(tab1); // Create the second tab and add a Label control TabItem tab2 = new TabItem(); tab2.Header = "Tab 2"; Label label2 = new Label(); label2.Content = "This is Tab 2"; tab2.Content = label2; tabControl.Items.Add(tab2);
// Create the TabControl TabControl tabControl = new TabControl(); // Create a data context and bind the TabControl to a collection of ViewModels ListIn this example, the ViewModel class has two properties: Header and Content, which are used to define the tab header and content. The TabControl is then bound to a collection of ViewModel objects and a DataTemplate is used to define the UI for each tab. The System.Windows.Controls TabControl is part of the WPF framework and is included in the WindowsBase.dll library.viewModels = new List (); viewModels.Add(new ViewModel("Tab 1", "This is Tab 1")); viewModels.Add(new ViewModel("Tab 2", "This is Tab 2")); tabControl.ItemsSource = viewModels; // Define the DataTemplate for each tab DataTemplate tabTemplate = new DataTemplate(); FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Label)); factory.SetBinding(Label.ContentProperty, new Binding("Content")); tabTemplate.VisualTree = factory; tabControl.ContentTemplate = tabTemplate;