【问题标题】:Is there any way to create new tabItem in TabControl WPF?有没有办法在 TabControl WPF 中创建新的 tabItem?
【发布时间】:2022-12-22 03:35:28
【问题描述】:

我在 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 有点 dirrenet。
我可以更改 WPF 的代码吗?或其他方式..?谢谢你的帮助。

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

    你可以这样做:

    假设你有这个 TabControlTabItem一种和一个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);
    }
    

    单击按钮后,您将被添加另一个 TabItem (C)

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 1970-01-01
      • 2018-04-21
      • 2012-09-07
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2011-03-31
      • 2012-02-18
      相关资源
      最近更新 更多