簡體   English   中英

有沒有辦法在 TabControl WPF 中創建新的 tabItem?

[英]Is there any way to create new tabItem in TabControl WPF?

我在 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 的代碼嗎? 或其他方式..? 謝謝你的幫助。

你可以這樣做:

假設您有這個帶有TabItem ABTabControl以及一個用於添加TabItemButton

<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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM