Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to add Tabs to a tab control in WPF but nothing appears on the control at runtime. I have tried following the examples I keep seeing. Right now this is what I have but it isn't working
_myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
MessageBox.Show(myReader["SectionName"].ToString());
TabItem newTabItem = new TabItem
Header = myReader["SectionName"].ToString(),
Name = myReader["SectionID"].ToString()
TabMain.Items.Add(newTabItem);
_myConnection.Close();
TabMain.SelectedIndex = 0;
You can add tabs dynamically by using the following code.
Add the following code to declare tab control instance globally.
TabControl tbControl;
Now, add the following code to the loaded event of the tab control.
private void tbCtrl_Loaded(object sender, RoutedEventArgs e)
tbControl = (sender as TabControl);
I have used a button to add new tabs for the existing tab control.
private void btnAdd_Click(object sender, RoutedEventArgs e)
TabItem newTabItem = new TabItem
Header = "Test",
Name = "Test"
tbControl.Items.Add(newTabItem);
Following is my tab control xaml view.
<TabControl x:Name="tbCtrl" HorizontalAlignment="Left" Height="270" Margin="54,36,0,0" VerticalAlignment="Top" Width="524" Loaded="tbCtrl_Loaded">
<TabItem Header="Tab - 01">
<Grid Background="#FFE5E5E5">
<Button x:Name="btnAdd" Content="Add New Tab" HorizontalAlignment="Left" Margin="68,95,0,0" VerticalAlignment="Top" Width="109" Height="29" Click="btnAdd_Click"/>
</Grid>
</TabItem>
</TabControl>
Finally, using this you can add any amount of tabs dynamically to the existing tab control.
Hope this fulfill your need.
Perhaps something in your DB values? I just wrote the most trivial of for loops to test, and this works fine (using just a TabControl and OnLoaded event on the XAML):
private void Window_Loaded(object sender, RoutedEventArgs e)
for (int i = 1; i <= 3; i++)
var item = new TabItem {Header = i.ToString(), Name = $"tab{i}"};
TabMain.Items.Add(item);
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.