Home > Back-end >  Adding a character to a tab title when modifying text
Adding a character to a tab title when modifying text

Time:03-14

i'm working on making a notepad equivalent in C# using MVVM design pattern for an university assignment. I've created the tabs successfully but now I have a problem adding the little "*" to the tabname when the content changes from the original and making it disappear upon saving. How can this be implemented ?

Here is the code for the tabcontrol:

<TabControl Margin="10,26,10,10" Grid.Column="2" ItemsSource="{Binding FileTabs}" SelectedIndex="{Binding CurrentSelectedTab}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding FileTabName}" />
                        <Button Command="{Binding Close, 
                            RelativeSource={RelativeSource AncestorType={x:Type local:FileMenuCommands}}, 
                            Mode=TwoWay, 
                            UpdateSourceTrigger=PropertyChanged}"  Width="20" Height="20" Content="X"/>
                    </StackPanel>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding FileTabContent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AcceptsReturn="True" AcceptsTab="True" />
                </DataTemplate>
            </TabControl.ContentTemplate>
</TabControl>

The model for the tabfile:

using System;
using System.Collections.Generic;
using System.Text;

namespace Notepad___.Model
{
    class FileTabProvider
    {
        public string FileTabName { get; set; }
        public string FileFullPath { get; set; }
        public string FileTabContent { get; set; }

        public FileTabProvider(string FileTabName, string FileFullPath, string FileTabContent)
        {
            this.FileTabName = FileTabName;
            this.FileFullPath = FileFullPath;
            this.FileTabContent = FileTabContent;
        }
    }
}

Also the two save functions created in the view model of the mainwindow:


        private void SaveFile(object parameter)
        {
            if (FileTabs[CurrentSelectedTab].FileFullPath == "")
                SaveAsFile(parameter);
            else
                File.WriteAllText(FileTabs[CurrentSelectedTab].FileFullPath, FileTabs[CurrentSelectedTab].FileTabContent.ToString());
        }

        private void SaveAsFile(object parameter)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (saveFileDialog.ShowDialog() == true)
                File.WriteAllText(saveFileDialog.FileName, FileTabs[CurrentSelectedTab].FileTabContent.ToString());
        }

CodePudding user response:

You can do it this way:

<Grid>
    <TabControl>
        <TabItem Header="ABC" x:Name="TabItem1" KeyDown="TabItem1_OnKeyDown">
            <TabItem.Content>
                <Grid>
                    <TextBox Text="{Binding YourTextProp}" TextChanged="TextBoxBase_OnTextChanged"/>
                </Grid>
            </TabItem.Content>
        </TabItem>
    </TabControl>
</Grid>  




public MainWindow()
{
    InitializeComponent();
}

private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
{
    if (TabItem1.Header is string tabItemHeader && !tabItemHeader.Contains("*"))
    {
        tabItemHeader  = "*";
        TabItem1.Header = tabItemHeader;
    }
}

private void TabItem1_OnKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control && TabItem1.Header is string tabItemHeader)
    {
        tabItemHeader = tabItemHeader.Substring(0, tabItemHeader.Length - 1);
        TabItem1.Header = tabItemHeader;
    }
}

CodePudding user response:

Implement the INotifyPropertyChanged interface in your view model and change the FileTabName property whenever the FileTabContent property is set. Something like this:

class FileTabProvider : INotifyPropertyChanged
{
    private string _originalFileTabName;

    private string _fileTabName;
    public string FileTabName
    {
        get { return _fileTabName; }
        set { _fileTabName = value; OnPropertyChanged(nameof(FileTabName)); }
    }

    public string FileFullPath { get; set; }

    private string _fileTabContent;
    public string FileTabContent
    {
        get { return _fileTabContent; }
        set
        { 
            _fileTabContent = value;
            FileTabName  = "*";
        }
    }

    public FileTabProvider(string fileTabName, string fileFullPath, string fileTabContent)
    {
        _fileTabName = fileTabName;
        FileFullPath = fileFullPath;
        _fileTabContent = fileTabContent;
    }

    public void Save() => FileTabName = _originalFileTabName;

    public event PropertyChangedEventHandler? PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Whenever you save, you need to remember to reset the filename:

private void SaveFile(object parameter)
{
    var tab = FileTabs[CurrentSelectedTab];

    if (tab.FileFullPath == "")
        SaveAsFile(parameter);
    else
        File.WriteAllText(tab.FileFullPath, tab.FileTabContent.ToString());

    tab.Save();
}
  • Related