try to make notepad++ with WPF

LooBi 141 Reputation points
2022-03-16T06:48:28.447+00:00

1. Drap and Drop

183565-ezgifcom-gif-maker.gif

2. can add Tab with button

183540-ezgifcom-gif-maker-1.gif

**I tried to mix them. but Tab2 is not appeared immediately. I have to click button.
Can anyboy help this problem? Thank you! **

183611-ezgifcom-gif-maker-2.gif

  • MainWindow.xaml <Window x:Class="XMLEditor.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
    xmlns:local="clr-namespace:XMLEditor"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Grid>
    <StackPanel>
    <Menu>
    <MenuItem Header="_File">
    <MenuItem Header="_New" />
    <MenuItem Header="_Save" />
    <MenuItem Header="_Quit" />
    </MenuItem>
    <MenuItem Header="_Edit">
    <MenuItem Header="_Copy" />
    <MenuItem Header="_Cut" />
    <MenuItem Header="_Paste" />
    </MenuItem>
    <MenuItem Header="_Help">
    <MenuItem Header="_Test" />
    </MenuItem>
    </Menu>
    <TabControl Name="tabDynamic" ItemsSource="{Binding}" SelectionChanged="tabDynamic_SelectionChanged"
    AllowDrop="True" PreviewDragOver="tabDynamic_PreviewDragOver" PreviewDrop="tabDynamic_PreviewDrop">
    <TabControl.Resources>
    <DataTemplate x:Key="TabHeader" DataType="TabItem">
    <DockPanel>
    <Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" Click="btnDelete_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}">
    <Image Source="Images/x.png" Width="11" Height="11"></Image>
    </Button>
    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Header}" />
    </DockPanel>
    </DataTemplate>
    <Style TargetType="TextBox">
    <Setter Property="VerticalAlignment" Value="Stretch"></Setter>
    <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
    <Setter Property="AcceptsReturn" Value="True"></Setter>
    <Setter Property="TextWrapping" Value="WrapWithOverflow"></Setter>
    <Setter Property="MaxLines" Value="5000"></Setter>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"></Setter>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"></Setter>
    </Style>
    </TabControl.Resources>
    </TabControl>
    </StackPanel>
    </Grid>
    </Window>
  • MainWindow.xaml.cs using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using ICSharpCode.AvalonEdit; namespace XMLEditor
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    private List<TabItem> _tabItems;
    private TabItem _tabAdd;
           public MainWindow()   
           {  
               try  
               {  
                   InitializeComponent();  
    
                   // initialize tabItem array  
                   _tabItems = new List<TabItem>();  
    
                   // add a tabItem with + in header   
                   _tabAdd = new TabItem();  
                   _tabAdd.Header = "+";  
                   // tabAdd.MouseLeftButtonUp += new MouseButtonEventHandler(tabAdd_MouseLeftButtonUp);  
    
                   _tabItems.Add(_tabAdd);  
    
                   // add first tab  
                   this.AddTabItem();  
    
                   // bind tab control  
                   tabDynamic.DataContext = _tabItems;  
    
                   tabDynamic.SelectedIndex = 0;  
               }  
               catch (Exception ex)  
               {  
                   MessageBox.Show(ex.Message);  
               }  
           }  
    
           private TabItem AddTabItem()  
           {  
               int count = _tabItems.Count;  
    
               // create new tab item  
               TabItem tab = new TabItem();  
    
               tab.Header = string.Format("Tab {0}", count);  
               tab.Name = string.Format("tab{0}", count);  
               tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;  
    
               tab.MouseDoubleClick += new MouseButtonEventHandler(tab_MouseDoubleClick);  
    
               // add controls to tab item, this case I added just a textbox  
               TextBox txt = new TextBox();  
               txt.Name = "txt";  
    
               tab.Content = txt;  
    
               // insert tab item right before the last (+) tab item  
               _tabItems.Insert(count - 1, tab);  
    
               return tab;  
           }  
    
           private void tabAdd_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)  
           {  
               // clear tab control binding  
               tabDynamic.DataContext = null;  
    
               TabItem tab = this.AddTabItem();  
    
               // bind tab control  
               tabDynamic.DataContext = _tabItems;  
    
               // select newly added tab item  
               tabDynamic.SelectedItem = tab;  
           }  
    
           private void tab_MouseDoubleClick(object sender, MouseButtonEventArgs e)  
           {  
               TabItem tab = sender as TabItem;  
    
               TabProperty dlg = new TabProperty();  
    
               // get existing header text  
               dlg.txtTitle.Text = tab.Header.ToString();  
    
               if (dlg.ShowDialog() == true)  
               {  
                   // change header text  
                   tab.Header = dlg.txtTitle.Text.Trim();  
               }  
           }  
    
           private void tabDynamic_SelectionChanged(object sender, SelectionChangedEventArgs e)  
           {  
               TabItem tab = tabDynamic.SelectedItem as TabItem;  
               if (tab == null) return;  
    
               if (tab.Equals(_tabAdd))  
               {  
                   // clear tab control binding  
                   tabDynamic.DataContext = null;  
    
                   TabItem newTab = this.AddTabItem();  
    
                   // bind tab control  
                   tabDynamic.DataContext = _tabItems;  
    
                   // select newly added tab item  
                   tabDynamic.SelectedItem = newTab;  
               }  
               else  
               {  
                   // your code here...  
               }  
           }  
           private void tabDynamic_PreviewDragOver(object sender, DragEventArgs e)  
           {  
               if (e.Data.GetDataPresent(DataFormats.FileDrop))  
               {  
                   e.Effects = DragDropEffects.All;  
               }  
               else  
               {  
                   e.Effects = DragDropEffects.None;  
               }  
               e.Handled = false;  
           }  
    
           private void tabDynamic_PreviewDrop(object sender, DragEventArgs e)  
           {  
               string[] fileloadup = (string[])e.Data.GetData(DataFormats.FileDrop);  
    
               e.Handled = true;  
               this.AddTabItem();  
           }  
    
           private void btnDelete_Click(object sender, RoutedEventArgs e)  
           {  
               string tabName = (sender as Button).CommandParameter.ToString();  
    
               var item = tabDynamic.Items.Cast<TabItem>().Where(i => i.Name.Equals(tabName)).SingleOrDefault();  
    
               TabItem tab = item as TabItem;  
    
               if (tab != null)  
               {  
                   if (_tabItems.Count < 3)  
                   {  
                       MessageBox.Show("Cannot remove last tab.");  
                   }  
                   else if (MessageBox.Show(string.Format("Are you sure you want to remove the tab '{0}'?", tab.Header.ToString()),  
                       "Remove Tab", MessageBoxButton.YesNo) == MessageBoxResult.Yes)  
                   {  
                       // get selected tab  
                       TabItem selectedTab = tabDynamic.SelectedItem as TabItem;  
    
                       // clear tab control binding  
                       tabDynamic.DataContext = null;  
    
                       _tabItems.Remove(tab);  
    
                       // bind tab control  
                       tabDynamic.DataContext = _tabItems;  
    
                       // select previously selected tab. if that is removed then select first tab  
                       if (selectedTab == null || selectedTab.Equals(tab))  
                       {  
                           selectedTab = _tabItems[0];  
                       }  
                       tabDynamic.SelectedItem = selectedTab;  
                   }  
               }  
           }  
    
    
       }  
    
    }
  • TabProperty.xaml <UserControl x:Class="XMLEditor.TabProperty"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:XMLEditor"
    xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
    mc:Ignorable="d"
    d:DesignHeight="450" d:DesignWidth="800">
    <Grid Margin="4">
    <Grid.RowDefinitions>
    <RowDefinition></RowDefinition>
    <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="60px"></ColumnDefinition>
    <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.Resources>
    <Style TargetType="Label">
    <Setter Property="HorizontalAlignment" Value="Right"></Setter>
    </Style>
    </Grid.Resources>
    <Label Content="Tab Title:" Grid.Row="0" Grid.Column="0" Height="25" />
           <avalonedit:TextEditor   
               x:Name="txtTitle"  
               Grid.Row="0" Grid.Column="1" Height="100"  
               FontFamily="Consolas"  
               FontSize="10pt"  
               SyntaxHighlighting="C#"  
               ShowLineNumbers="True"  
               SelectionLength="0" Margin="0, 0, 0, -7"/>  
    
           <StackPanel Grid.Row="1" Grid.ColumnSpan="2" Orientation="Horizontal">  
               <Button Content="OK" Name="btnOK" IsDefault="True" Height="23" Click="btnOK_Click"></Button>  
               <Button Content="Cancel" Name="btnCancel" IsCancel="True" Height="23"></Button>  
           </StackPanel>  
       </Grid>  
    
    </UserControl>
  • TabPropety.xaml.cs using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using ICSharpCode.AvalonEdit;
    namespace XMLEditor
    {
    /// <summary>
    /// TabProperty.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class TabProperty : UserControl
    {
    public TabProperty()
    {
    InitializeComponent();
    }
           public bool DialogResult { get; private set; }  
    
           private void btnOK_Click(object sender, RoutedEventArgs e)  
           {  
               this.DialogResult = true;  
           }  
    
           internal bool ShowDialog()  
           {  
               throw new NotImplementedException();  
           }  
       }  
    
    }
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,676 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,277 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.
766 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,266 Reputation points Microsoft Vendor
    2022-03-21T08:50:39.673+00:00

    I modified tabDynamic_PreviewDrop method, it can add TabItem by dragging Item to _tabAdd .

     private void tabDynamic_PreviewDrop(object sender, DragEventArgs e)  
        {  
          tabDynamic.DataContext = null;  
    
          string[] fileloadup = (string[])e.Data.GetData(DataFormats.FileDrop);  
    
    
          int count = _tabItems.Count;  
          TabItem tab = new TabItem();  
          tab.Header = string.Format("Tab {0}", count);  
          tab.Name = string.Format("tab{0}", count);  
          tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;  
          tab.MouseDoubleClick += new MouseButtonEventHandler(tab_MouseDoubleClick);  
          tab.Content = File.ReadAllText(fileloadup[0]);  
          _tabItems.Insert(count - 1, tab);  
          tabDynamic.DataContext = _tabItems;  
          tabDynamic.SelectedItem = tab;  
    
          e.Handled = true;  
        }  
    

    The result:
    185127-6.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful