带有标签项的用户控件未显示

时间:2019-01-11 13:42:36

标签: c# wpf user-controls tabcontrol tabitem

我有我的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);

WithoutTabControl

编辑:

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

1 个答案:

答案 0 :(得分:0)

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>