修改文本时向选项卡标题添加字符

Adding a character to a tab title when modifying text

我正在为一项大学作业使用 MVVM 设计模式在 C# 中制作一个等效的记事本++。我已经成功创建了选项卡,但现在我在将小“*”添加到选项卡名称时遇到问题,当内容从原始内容更改并使其在保存时消失时。如何实施?

这是选项卡控件的代码:

<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>

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;
        }
    }
}

还有在主窗口的视图模型中创建的两个保存函数:


        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());
        }

你可以这样做:

<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;
    }
}

在您的视图模型中实现 INotifyPropertyChanged 接口,并在设置 FileTabContent 属性 时更改 FileTabName 属性。像这样:

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));
}

每次保存时,记得重新设置文件名:

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

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

    tab.Save();
}