I am trying to make a program where tabs can be added via C# code. The code I used adds the tab correctly, but the contents are always centered. Below is the code I am using:
void AddTab(UserControl contents, string header)
{
UserControl tabContents = contents;
tabContents.HorizontalAlignment = HorizontalAlignment.Stretch;
tabContents.HorizontalContentAlignment = HorizontalAlignment.Stretch;
tabContents.VerticalAlignment = VerticalAlignment.Stretch;
tabContents.VerticalContentAlignment = VerticalAlignment.Stretch;
tabContents.Margin = new Thickness(0, 20, 0, 19);
Grid contentGrid = new Grid();
contentGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
contentGrid.VerticalAlignment = VerticalAlignment.Stretch;
contentGrid.Children.Add(tabContents);
tabMain.Items.Add(new TabItem
{
Header = header,
Content = contentGrid,
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalContentAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
VerticalContentAlignment = VerticalAlignment.Stretch,
IsSelected = true
});
}
The XAML for the UserControl I am trying to add is below (class names/URLs showing the name of my program and a news URL have been replaced with generic terms):
<UserControl x:Class="ClassName"
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="namespace"
mc:Ignorable="d" Width="840" Height="475">
<Grid Background="White">
<Label Content="content" VerticalAlignment="Top" RenderTransformOrigin="0.132,0.192" HorizontalContentAlignment="Center" FontSize="24"/>
<ListBox Height="397" Margin="0,68,0,0" VerticalAlignment="Top" Width="210" HorizontalAlignment="Left" HorizontalContentAlignment="Stretch" RenderTransformOrigin="0.366,0.527"/>
<Label Content="Recent Projects:" HorizontalAlignment="Left" Margin="0,42,0,0" VerticalAlignment="Top"/>
<WebBrowser Margin="0,42,0,0" Width="290" HorizontalAlignment="Right" Uid="url"/>
</Grid>
The UserControl resizes properly in the Designer, and the other tabs inside of my TabControl, as well as the TabControl itself, resize properly, but the new content does not. My TabControl code snippet is below:
<TabControl x:Name="tabMain" Margin="0,25,0,19" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
How can I make it so the contents of the tab added using the C# code are automatically resized when the main window is resized/maximized?
The UserControl doesn't resize because you fixed the width and height properties for the controls inside. You also define their positions via "Margin" which is not the best way to do it in WPF.
The proper way to set the relative positions of the controls is using Grid with Grid.RowDefinition and Grid.ColumnDefinition or StackPanels/DockPanel.
I changed the code you provided to make everything resize with the Window and added colored Borders to make it obvious:
code behind:
Xaml of the UserControl:
This article explains how to set up a Layout pretty well: https://www.wpftutorial.net/LayoutProperties.html
Let me know if you have questions :)