将选项卡添加到 WPF C# 中的现有选项卡控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33348925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:30:24  来源:igfitidea点击:

Add tabs to an existing tab control in WPF C#

c#wpftabs

提问by Jeebwise

I am trying to add Tabs to a tab control in WPF but nothing appears on the control at runtime. I have tried following the examples I keep seeing. Right now this is what I have but it isn't working

我正在尝试将选项卡添加到 WPF 中的选项卡控件,但在运行时控件上没有显示任何内容。我尝试按照我一直看到的示例进行操作。现在这就是我所拥有的,但它不起作用

_myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
    MessageBox.Show(myReader["SectionName"].ToString());
    TabItem newTabItem = new TabItem
    {
        Header = myReader["SectionName"].ToString(),
        Name = myReader["SectionID"].ToString()
    };
    TabMain.Items.Add(newTabItem);
}
_myConnection.Close();
TabMain.SelectedIndex = 0;

回答by Suvethan Nantha

You can add tabs dynamically by using the following code.

您可以使用以下代码动态添加选项卡。

Add the following code to declare tab control instance globally.

添加以下代码以全局声明选项卡控件实例。

TabControl tbControl;

Now, add the following code to the loaded event of the tab control.

现在,将以下代码添加到选项卡控件的加载事件中。

private void tbCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            tbControl = (sender as TabControl);
        }

I have used a button to add new tabs for the existing tab control.

我使用按钮为现有选项卡控件添加新选项卡。

private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            TabItem newTabItem = new TabItem
            {
                Header = "Test",
                Name = "Test"
            };
            tbControl.Items.Add(newTabItem);
        }

Following is my tab control xaml view.

以下是我的选项卡控件 xaml 视图。

<TabControl  x:Name="tbCtrl" HorizontalAlignment="Left" Height="270" Margin="54,36,0,0" VerticalAlignment="Top" Width="524" Loaded="tbCtrl_Loaded">
            <TabItem Header="Tab - 01">
                <Grid Background="#FFE5E5E5">
                    <Button x:Name="btnAdd" Content="Add New Tab" HorizontalAlignment="Left" Margin="68,95,0,0" VerticalAlignment="Top" Width="109" Height="29" Click="btnAdd_Click"/>
                </Grid>
            </TabItem>
        </TabControl>

Finally, using this you can add any amount of tabs dynamically to the existing tab control.

最后,使用它您可以动态地向现有的选项卡控件添加任意数量的选项卡。

Hope this fulfill your need.

希望这能满足您的需求。

回答by Todd Sprang

Perhaps something in your DB values? I just wrote the most trivial of for loops to test, and this works fine (using just a TabControl and OnLoaded event on the XAML):

也许你的数据库值中有什么?我只是编写了最简单的 for 循环来测试,这很好用(仅在 XAML 上使用 TabControl 和 OnLoaded 事件):

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 1; i <= 3; i++)
        {
            var item = new TabItem {Header = i.ToString(), Name = $"tab{i}"};
            TabMain.Items.Add(item);
        }
    }