It's difficult to set multiple TabItems in a single xaml.
If you just want to make your code more manageable, then I recommend defining the data for each tab in the user control, but still have the TabItem in the main tab control.
You could decompose the tab content into UserControl:
<UserControl x:Class="TabControlItemFromDifferentView.View1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TabControlItemFromDifferentView"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Label Content="tab1"/>
</Grid>
</UserControl>
If you want to include a TabItem in a user control, you can do so by first creating the user control and then changing the type of the user control to the TabItem type (make sure in the xaml root node and in the code behind).
The "User Control" for each TabItem1 is TabItem type:
<TabItem x:Class="TabControlItemFromDifferentView.View2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TabControlItemFromDifferentView"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="Tab Data" />
</Grid>
</TabItem>
Code behind:
public partial class View2 : TabItem
{
public View2()
{
InitializeComponent();
}
}
MainWindow.xaml:
<Grid>
<TabControl>
<TabItem >
<local:View1/>
</TabItem>
<local:View2 Header="tab2"/>
<TabItem >
<Label Content="tab3"/>
</TabItem>
</TabControl>
</Grid>
Or you can see if the solution here works for you.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.