Search code examples
c#wpfuser-controlstabcontroltabitem

User Control with tab item doesn't show


I have my UserControl TableTab. In my application, I have a TabControl and I want to add my TableTab to my to my TabControl at runtime via a button. The problem is that it doesn't show the layout, just a grey color. Only when I added a TabControl directly to my TabItem it worked. Does it have to do something with adding controls at runtime or why doesn't it show the layout properly?

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); }
    }
}

And here I add my TableTab to my existing TabControl

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

m.TabContTables.Items.Add(t);

WithoutTabControl

EDIT:

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

Solution

  • TableTab should be a TabItem and not a UserControl. Just change the base type:

    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>