winforms 有没有办法在TabControl WPF中创建新的tabItem?

9udxz4iz  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(170)

我在WinForm里做了个代码,很管用
我拖动xml文件并放到tabPage。

private TextEditorControl AddNewTextEditor(string title)
        {
            var tab = new TabPage(title);
            var editor = new TextEditorControl();
            editor.Dock = System.Windows.Forms.DockStyle.Fill;
            editor.IsReadOnly = false;
            editor.Document.DocumentChanged += 
                new DocumentEventHandler((sender, e) => { SetModifiedFlag(editor, true); });
            // When a tab page gets the focus, move the focus to the editor control
            // instead when it gets the Enter (focus) event. I use BeginInvoke 
            // because changing the focus directly in the Enter handler doesn't 
            // work.
            tab.Enter +=
                new EventHandler((sender, e) => { 
                    var page = ((TabPage)sender);
                    page.BeginInvoke(new Action<TabPage>(p => p.Controls[0].Focus()), page);
                });
            tab.Controls.Add(editor);
            fileTabs.Controls.Add(tab);

            if (_editorSettings == null) {
                _editorSettings = editor.TextEditorProperties;
                OnSettingsChanged();
            } else
                editor.TextEditorProperties = _editorSettings;
            return editor;
        }

但是WPF有点脏。
我可以改变代码为WPF??或其他方式..?谢谢你的帮助。

gmxoilav

gmxoilav1#

您可以这样做:
假设您有一个TabControl,其中包含TabItemAB以及一个Button,用于添加TabItem

<StackPanel>
    <TabControl x:Name="TabControl">
        <TabItem Header="A"/>
        <TabItem Header="B"/>
    </TabControl>
    <Button Content="Add New Tab Item" Click="ButtonBase_OnClick"/>
</StackPanel>

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    var tabItem = new TabItem { Header = "C" };
    TabControl.Items.Add(tabItem);
}

单击该按钮后,将添加另一个选项卡项(C)

相关问题