【问题标题】:c# tabcontrol and grid problemc# tabcontrol和grid问题
【发布时间】:2011-04-01 00:04:21
【问题描述】:

您好,我有一个这样的 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>

如果我单击更多该按钮,将继续添加这样的选项卡。

请帮助我(崇拜)

【问题讨论】:

    标签: c# wpf tabcontrol


    【解决方案1】:

    鉴于这是您的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-06
      • 2021-10-04
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多