How to add buttons on different tabs using TabControl in WPF
-
20-09-2019 - |
Question
I have the basic tabs made and I want each tab to have a different set of buttons, but the button is added to the main window and no the tab. When i go to the code and cut the button and paste it to the tab I want it to be, it works but only for 1 button, then for the second button I get an error message:
"The object TabItem
already has a child and cannot add Button.TabItem
can accept only one child."
This is how the code looks like:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="394" Width="570">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0*" />
<ColumnDefinition Width="548*" />
</Grid.ColumnDefinitions>
<TabControl Margin="9,30,11,10" Name="tabControl1" Grid.Column="1" SelectionChanged="tabControl1_SelectionChanged">
<TabItem Name="Admin" Header="Admin">
</TabItem>
<TabItem Name="Staff" Header="Staff" />
<TabItem Name="Student" Header="Student" />
<TabItem Name="Diary" Header="Diary" />
<TabItem Name="Timetables" Header="Timetables" />
<TabItem Name="Assignments" Header="Assignments" />
<TabItem Name="Courses" Header="Courses" />
</TabControl>
<Menu Height="16" Name="menu1" VerticalAlignment="Top" Grid.Column="1" />
<Button Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="18,64,0,0" Name="button1" VerticalAlignment="Top" Width="118">Add/Edit Student</Button>
</Grid>
I want to implement the "button1" inside of the "Admin" tab
It works but it fails when I do a second button
Thanks in advance
OTHER TIPS
Add a <StackPanel>,<Grid>, <Border>,
etc.--any Element that can contain multiple children, just like you would a <Window>
This holds true for ANY wpf element that can only accept a single child. Button, ToggleButton, etc.
<TabItem>
<Grid>
.......
</Grid>
</TabItem
You are getting that error because TabItem can only contain a single UIElement as a child. You will want to use some kind of panel (i.e. Grid, DockPanel, StackPanel, Canvas...) to arrange all of your UIElements.