You can try to refer to the code below.
MainWindow.xaml:
<StackPanel>
<TabControl Background="LightGreen" TabStripPlacement="Top" ItemsSource="{Binding Titles, Mode=TwoWay}" Height="200" AllowDrop="True"
PreviewDragOver="TabControl_PreviewDragOver" PreviewDrop="TabControl_PreviewDrop" >
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</StackPanel>
MainWidnow.xaml.cs:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
namespace TabControlDragToCteateNewPage
{
public partial class MainWindow : Window
{
static int tabs = 1;
MainWindowViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new MainWindowViewModel();
DataContext=vm;
}
private void TabControl_PreviewDrop(object sender, DragEventArgs e)
{
string[] fileloadup = (string[])e.Data.GetData(DataFormats.FileDrop);
var item = new MainWindowViewModel.Item() { Header = "Tab " + tabs, Content = File.ReadAllText(fileloadup[0]) };
vm.Titles.Add(item);
e.Handled = true;
tabs++;
}
private void TabControl_PreviewDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.All;
}
else
{
e.Effects = DragDropEffects.None;
}
e.Handled = false;
}
}
public class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
Titles = new ObservableCollection<Item>();
}
public ObservableCollection<Item> Titles
{
get { return _titles; }
set
{
_titles = value;
OnPropertyChanged("Titles");
}
}
public class Item
{
public string Header { get; set; }
public string Content { get; set; }
}
private ObservableCollection<Item> _titles;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
The result:
Update:
<StackPanel>
<TabControl Background="LightGreen" TabStripPlacement="Top" ItemsSource="{Binding Titles, Mode=TwoWay}" Height="200" AllowDrop="True"
PreviewDragOver="TabControl_PreviewDragOver" PreviewDrop="TabControl_PreviewDrop" >
<TabControl.ItemTemplate>
<DataTemplate>
<TextBox Margin="10,0,10,0" Text="{Binding Header ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel x:Name="sp">
<TextBox Width="{Binding ElementName=sp ,Path=ActualWidth}" Height="{Binding ElementName=sp,Path=ActualHeight}" Text="{Binding Content}" />
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</StackPanel>
MainWindowViewModel :
public class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
Titles = new ObservableCollection<Item>();
}
public ObservableCollection<Item> Titles
{
get { return _titles; }
set
{
_titles = value;
OnPropertyChanged("Titles");
}
}
public class Item : INotifyPropertyChanged
{
private string header;
public string Header
{
get { return header; }
set
{
header = value;
OnPropertyChanged("Header");
}
}
private string content;
public string Content
{
get { return content; }
set
{
content = value;
OnPropertyChanged("Content");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
private ObservableCollection<Item> _titles;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.