How to create new pages to tabControl by drag and drop (WPF) ?

LooBi 141 Reputation points
2022-03-10T00:08:07.58+00:00

There is a tabControl and I want to see it by dragging and dropping the XML file there. like NotePad++
But I know how to drag and drop to a textbox to view.

private void textBox_PreviewDrop(object sender, DragEventArgs e)
        {
            string[] fileloadup = (string[])e.Data.GetData(DataFormats.FileDrop); // Get the filename including path
            textBox2.Text = File.ReadAllText(fileloadup[0]); // Load data to textBox
            e.Handled = true; // This is file being dropped not copied text so we handle it
        }

        private void textBox_PreviewDragOver(object sender, DragEventArgs e)
        {
            e.Effects = DragDropEffects.All;
            e.Handled = true;
        }

Thanks for your help!

This was translated by Google Translate.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
790 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,341 Reputation points Microsoft Vendor
    2022-03-10T07:40:11.337+00:00

    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:
    181793-7.gif

    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:
    182092-11.gif


    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful