'Is there any way to create new tabItem in TabControl WPF?
I made a code in WinForm. It works.
I dragged xml file and dropped to 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;
}
But WPF is a bit dirrenet.
Can I change the code for WPF?? or other way..? Thanks for your help.
Solution 1:[1]
You can do it this way:
Suppose you have this TabControl with TabItem A and B and a button for adding 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);
}
After clicking the button you will be added another TabItem (C)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ohad Cohen |
