c#tabcontrol和网格问题

时间:2011-03-31 15:57:09

标签: c# wpf tabcontrol

嗨我有像这样的xaml代码

    <Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="test.Window1"
    x:Name="Window"
    Title="Window1"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <Button Content="Create a tab" HorizontalAlignment="Left" Margin="49,26,0,0" VerticalAlignment="Top" Width="75"/>
        <TabControl Margin="0,63,0,0">

        </TabControl>
    </Grid>
</Window>

在TabControl中没有tabItem那里。 请帮助我,如何用c#编程: 如果我单击该按钮,它将添加一个带有网格和文本块的标签项。结果我希望如此:

<Grid x:Name="LayoutRoot">
    <Button Content="Create a tab" HorizontalAlignment="Left" Margin="49,26,0,0" VerticalAlignment="Top" Width="75"/>
    <TabControl Margin="0,63,0,0">
        <TabItem Header="tab1">
            <Grid>
                <TextBlock Text="hi there" />
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

如果我点击更多按钮,将继续添加这样的标签。

请帮助我(敬拜)

1 个答案:

答案 0 :(得分:3)

鉴于这是你的xaml:

<Grid x:Name="LayoutRoot">
    <Button Content="Create a tab" HorizontalAlignment="Left" Margin="49,26,0,0" VerticalAlignment="Top" Width="75"/>
    <TabControl Margin="0,63,0,0" x:Name="MyTabControl">
        <TabItem Header="tab1">
            <Grid>
                <TextBlock Text="hi there" />
            </Grid>
        </TabItem>
    </TabControl>
</Grid>

你可以在代码隐藏中添加tabitem,如下所示:

TextBlock t = new TextBlock { Text= "hi" };
Grid g = new Grid;
g.Children.Add(t);
TabItem t = new TabItem();
t.Content = g;
MyTabControl.Children.Add(t);