简体   繁体   English

使通过 C# 代码添加的 TabItems 自动调整大小

[英]Make TabItems Added Through C# Code Resize Automatically

I am trying to make a program where tabs can be added via C# code.我正在尝试制作一个可以通过 C# 代码添加选项卡的程序。 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 的 XAML 如下(显示我的程序名称的类名/URL 和新闻 URL 已被通用术语替换):

<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. UserControl 在设计器中正确调整大小,我的 TabControl 内的其他选项卡以及 TabControl 本身都正确调整大小,但新内容没有。 My TabControl code snippet is below:我的 TabControl 代码片段如下:

<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?当主 window 调整大小/最大化时,如何自动调整使用 C# 代码添加的选项卡的内容?

The UserControl doesn't resize because you fixed the width and height properties for the controls inside. UserControl 不会调整大小,因为您固定了内部控件的宽度和高度属性。 You also define their positions via "Margin" which is not the best way to do it in WPF.您还可以通过“保证金”定义他们的位置,这在 WPF 中并不是最好的方法。

The proper way to set the relative positions of the controls is using Grid with Grid.RowDefinition and Grid.ColumnDefinition or StackPanels/DockPanel.设置控件相对位置的正确方法是将 Grid 与 Grid.RowDefinition 和 Grid.ColumnDefinition 或 StackPanels/DockPanel 一起使用。

I changed the code you provided to make everything resize with the Window and added colored Borders to make it obvious:我更改了您提供的代码以使用 Window 调整所有内容,并添加了彩色边框以使其明显:

code behind:后面的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private int nbTest = 0;

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.nbTest++;
        this.AddTab(new UserControl1(), "header" + this.nbTest.ToString());
    }

    void AddTab(UserControl tabContent, string header)
    {
        tabContent.Margin = new Thickness(0, 20, 0, 19);
        //You don't need this part unless you intend to add more content to the Grid than just the UserControl
        /*Grid contentGrid = new Grid();
        contentGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
        contentGrid.VerticalAlignment = VerticalAlignment.Stretch;
        contentGrid.Children.Add(tabContents);*/
    
        tabMain.Items.Add(new TabItem
        {
            Header = header,
            Content = tabContent,
            IsSelected = true
        });
    }
}

Xaml of the UserControl: UserControl 的 Xaml:

<UserControl x:Class="WpfApp19.UserControl1"
         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:WpfApp19"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Border BorderBrush="Blue" BorderThickness="1" Grid.Row="0">
        <!--If you remove the border, don't forget to put the Grid.Row="0" part on the Label -->
        <Label Content="Welcome to [program name]"/> HorizontalContentAlignment="Center" FontSize="24"/>
    </Border>
    <Grid Grid.Row="1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border BorderBrush="Green" BorderThickness="1" Grid.Column="0">
            <!--If you remove the border, don't forget to put the Grid.Column="0" part on the Grid -->
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <!--Instead of defining the rows for the Grid, you could place it in a StackPanel with an Orientation set to Vertical but the ListBox won't
                resize to fit the height available-->
                <Label Content="Recent Projects:" Grid.Row="0"/>
                <ListBox MinHeight="397" MinWidth="210" Grid.Row="1"/>
            </Grid>
        </Border>
        <Border BorderBrush="red" BorderThickness="1" Grid.Column="1">
            <!--If you remove the border, don't forget to put the Grid.Column="1" part on the WebBrowser -->
            <WebBrowser  Uid="url"/>
        </Border>

    </Grid>
</Grid>

This article explains how to set up a Layout pretty well: https://www.wpftutorial.net/LayoutProperties.html本文解释了如何很好地设置布局: https://www.wpftutorial.net/LayoutProperties.html

Let me know if you have questions:)如果您有任何问题,请告诉我:)

Have you tried wrapping the elements inside a view box?您是否尝试过将元素包装在视图框中?

 <Viewbox>

        <TabControl x:Name="tabMain" Padding="1920, 1080, 2080, 1080"
                    Margin="0,25,0,19"
                    HorizontalContentAlignment="Stretch"
                    VerticalContentAlignment="Stretch">
            <TabItem Header="TabItem" Height="100" Width="500"
                     FontSize="60">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="TabItem"
                     FontSize="60"
                     Height="100"
                     Width="500">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    </Viewbox>

I've added some padding so it might look a little different.我添加了一些填充,所以它看起来可能有点不同。 But from what I've tested this should resize the tab items when the user resizes the screen.但是根据我的测试,当用户调整屏幕大小时,这应该会调整选项卡项的大小。

Thanks,谢谢,

Also I'm quite new to SO and this is my 3rd answer here so if this doesn't work please tell me and I will delete it.另外,我对 SO 很陌生,这是我在这里的第三个答案,所以如果这不起作用,请告诉我,我会删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM