带有选项卡项的用户控件不显示

User Control with tab item doesn't show

我有我的 UserControl TableTab。在我的应用程序中,我有一个 TabControl,我想在运行时通过一个按钮将我的 TableTab 添加到我的 TabControl。问题是它没有显示布局,只是灰色。只有当我将 TabControl 直接添加到我的 TabItem 时它才有效。它是否必须在运行时添加控件或为什么不能正确显示布局?

TableTab.xaml

<UserControl x:Class="RestaurantManagmentSystemProject.TableTab"
         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:RestaurantManagmentSystemProject"
         Name="TableTabItem"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">

<TabItem Name="Tab" Header="{Binding TabItemHeader, ElementName=TableTab}">
    <DockPanel>
        <Grid DockPanel.Dock="Top" Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <DataGrid Grid.Column="0">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Order number"/>
                    <DataGridTextColumn Header="Customer"/>
                    <DataGridTextColumn Header="Total Price"/>
                    <DataGridComboBoxColumn Header="Urgency"/>
                </DataGrid.Columns>

            </DataGrid>

            <StackPanel Grid.Column="1">
                <Label Content="Maximum Seats:"/>
                <Label Content="Current connections:"/>
            </StackPanel>

            <DockPanel Grid.Column="2" LastChildFill="False" HorizontalAlignment="Right">
                <Button Name="BtnEditTable" Content="Edit Table" DockPanel.Dock="Top"/>
            </DockPanel>
        </Grid>

        <TabControl Margin="5,0,0,0">

        </TabControl>
    </DockPanel>
</TabItem>

TableTab.xaml.cs

public partial class TableTab : UserControl
{
    public static readonly DependencyProperty TableIdProperty;
    public static readonly DependencyProperty TabItemHeaderProperty;

    public TableTab()
    {
        InitializeComponent();
    }

    static TableTab()
    {
        TableIdProperty = DependencyProperty.Register("TableId", typeof(int), typeof(TableTab));
        TabItemHeaderProperty = DependencyProperty.Register("TabItemHeader", typeof(string), typeof(TableTab));
    }

    public int TableId
    {
        get { return (int)GetValue(TableIdProperty); }
        set { SetValue(TableIdProperty, value); }
    }

    public string TabItemHeader
    {
        get { return (string)GetValue(TabItemHeaderProperty); }
        set { SetValue(TabItemHeaderProperty, value); }
    }
}

在这里我将我的 TableTab 添加到我现有的 TabControl

TableTab t = new TableTab();
t.TabItemHeader = "Tab1";

m.TabContTables.Items.Add(t);

编辑:

MainWindow m;
<TabControl Grid.Column="1" Name="TabContTables">

TableTab 应该是 TabItem 而不是 UserControl。只需更改基本类型:

public partial class TableTab : TabItem
{
    public static readonly DependencyProperty TableIdProperty;
    public static readonly DependencyProperty TabItemHeaderProperty;

    public TableTab()
    {
        InitializeComponent();
        TabItemHeader = "head";
    }

    static TableTab()
    {
        TableIdProperty = DependencyProperty.Register("TableId", typeof(int), typeof(TableTab));
        TabItemHeaderProperty = DependencyProperty.Register("TabItemHeader", typeof(string), typeof(TableTab));
    }

    public int TableId
    {
        get { return (int)GetValue(TableIdProperty); }
        set { SetValue(TableIdProperty, value); }
    }

    public string TabItemHeader
    {
        get { return (string)GetValue(TabItemHeaderProperty); }
        set { SetValue(TabItemHeaderProperty, value); }
    }
}

XAML:

<TabItem x:Class="RestaurantManagmentSystemProject.TableTab" 
             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" 
             Name="Tab"
             Header="{Binding TabItemHeader, RelativeSource={RelativeSource Self}}"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DockPanel>
        <Grid DockPanel.Dock="Top" Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <DataGrid Grid.Column="0">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Order number"/>
                    <DataGridTextColumn Header="Customer"/>
                    <DataGridTextColumn Header="Total Price"/>
                    <DataGridComboBoxColumn Header="Urgency"/>
                </DataGrid.Columns>

            </DataGrid>

            <StackPanel Grid.Column="1">
                <Label Content="Maximum Seats:"/>
                <Label Content="Current connections:"/>
            </StackPanel>

            <DockPanel Grid.Column="2" LastChildFill="False" HorizontalAlignment="Right">
                <Button Name="BtnEditTable" Content="Edit Table" DockPanel.Dock="Top"/>
            </DockPanel>
        </Grid>

        <TabControl Margin="5,0,0,0">

        </TabControl>
    </DockPanel>
</TabItem>