Domanda

I am implementing a text editor in WPF and I have a reached a stage where I can open multiple files in tabs in my application. The code is as below. Now I need to implement the save feature for my text editor. For this I need to know which particular Tab is active for me to save that File alone. How will I do this functionality? Also my tabs do not have a close button('X'). How will I implement a close button functionality in WPF? Any help is appreciated.

public partial class MainWindow : Window
            {
                public MainWindow()
                {
                    InitializeComponent();  
                } 

private void Open_Click(object sender, RoutedEventArgs e)
        {    
 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

                dlg.DefaultExt = ".txt";
                dlg.Filter = "Text Files(*.txt)|*.txt";

                Nullable<bool> result = dlg.ShowDialog();
                mode openfile = mode.openFile;

                if (result.HasValue == true)
                {
                    if (File.Exists(dlg.FileName))
                        AddTabitem(dlg.FileName, openfile);
                    else
                        MessageBox.Show("NoSuch file exsists", "OpenError");

                }
            }

            private void AddTabitem(string filePath, mode fileMode) 
            {
                    TextRange range;
                    FileStream fStream;

                    if (fileMode == mode.openFile)
                    {
                        if (File.Exists(filePath))
                        {
                            RichTextBox mcRTB = new RichTextBox();
                            rtbList.Add(mcRTB);
                            TabItem tab = new TabItem();

                            try
                            {
                                range = new TextRange(mcRTB.Document.ContentStart, mcRTB.Document.ContentEnd);
                                fStream = new FileStream(filePath, FileMode.OpenOrCreate);
                                range.Load(fStream, DataFormats.Text);
                                fStream.Close();
                            }
                            catch(Exception)
                            {
                                MessageBox.Show("Unable to open specified file.", "Open File error", MessageBoxButton.OK);
                            }

                            tab.Header = ExtractFileName(filePath);
                            tab.Content = mcRTB;

                            EditorTabcontrol.Items.Insert(EditorTabcontrol.Items.Count, tab);
                            EditorTabcontrol.SelectedIndex = EditorTabcontrol.Items.Count - 1;    
                        }
                    }
        }

<ScrollViewer  VerticalScrollBarVisibility="Auto">
                <TabControl Width="648" Name="EditorTabcontrol" AllowDrop="True"/>
            </ScrollViewer>
È stato utile?

Soluzione

The EditorTabcontrol (of type TabControl) has a SelectedItem property, you can use that to identify which editor is currently active. From there you can get it's content.

To add a close (x) button you need can set the TabItem's Header property to a StackPanel (a horizontal one) instead of a simple string, and add a string (the filename) and button (with the text X or an appropriate image) to the Stackpanel. Then handle the Click event for the button.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top