1

我创建了一个 C# WPF 应用程序,其 RibbonApplicationMenu 显示最近使用 (MRU) 列表。不幸的是,当我从列表中选择现有文件或上传新文件时,显示不会更新。在 XAML 中,我有:

<local:MostRecentFiles x:Key="MostRecentFilesData" />
    ...
<ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
    <ribbon:RibbonGallery Name="RecentDocuments" CanUserFilter="False" 
        SelectedValue="{Binding MostRecentFile, UpdateSourceTrigger=PropertyChanged}">
        <ribbon:RibbonGalleryCategory Header="Recent Documents"
            ItemsSource="{DynamicResource MostRecentFilesData}">
        </ribbon:RibbonGalleryCategory>
    </ribbon:RibbonGallery>
</ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>

DataContext 被设置为一个包含

private ObservableCollection<string> _mostRecentFile = new ObservableCollection<string>();
public ObservableCollection<string> MostRecentFile
{
    get { return _mostRecentFile; }
    set
    {
        _mostRecentFile = value;
        OnPropertyChanged("MostRecentFile");
    }
}

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

在 OpenFile 例程中,代码是

MostRecentFiles mrf = new MostRecentFiles();
mrf.AddMRUitem(openFileDlg.FileName);

MostRecentFiles 类包含主要的类方法,并且我在代码中放置了一些示例文件路径。

public class MostRecentFiles : ObservableCollection<string>
{
    public ObservableCollection<string> MRUmenuItems = new ObservableCollection<string>();
    public MostRecentFiles()
    {
        AddMRUitem(@"C:\MyDocuments\File3.txt"); //
        AddMRUitem(@"C:\MyDocuments\File2.txt"); // } Sample files
        AddMRUitem(@"C:\MyDocuments\File1.txt"); //
    }

    public void AddMRUitem(string filePath)
    {
        int result;
        result = MRUmenuItems.IndexOf(filePath);
        if (result != -1)
        {
            MRUmenuItems.Remove(filePath);
            MRUmenuItems.Insert(0, filePath);
        }
        else
            AddMenuItem(filePath);
        UpdateMRUList();
    }

    private void UpdateMRUList()
    {
        this.Clear();
        foreach (string filePath in MRUmenuItems)
        {
            this.Add(filePath);
        }
        //OnPropertyChanged("MostRecentFile"); // <= Error CS1503
    }

    private void AddMenuItem(string newMRUfile)
    {
        MRUmenuItems.Insert(0, newMRUfile);
        if (MRUmenuItems.Count > 10)
        {
            MRUmenuItems.RemoveAt(MRUmenuItems.Count - 1);
        }
    }
    private string _mostRecentFile = "";
    public string MostRecentFile
    {
        get { return _mostRecentFile; }
        set
        {
            if (_mostRecentFile == value) return;
            _mostRecentFile = value;
            AddMRUitem(_mostRecentFile);
            //OnPropertyChanged("MostRecentFile");
        }
    }
}

在UpdateMRUList ()中取消删除OnPropertyChanged会产生错误:错误 CS1503 Argument 1: cannot convert from 'string' to 'System.ComponentModel.PropertyChangedEventArgs'

当我启动程序时,菜单正确显示了三个文件,但是当我选择一个时,显示的顺序不会改变;我希望选定的文件移动到列表的顶部。同样,当我打开一个新文件时,文件名不会添加到 MRU。

但是,如果我单步执行代码,列表将以正确的顺序更新。我做错了什么?

4

1 回答 1

1

您正在绑定SelectedValue到一个集合。您不需要自定义集合。只需将一个添加ObservableCollection到您的视图模型并移动所选项目上的项目已更改:

查看型号:

private void OnSelectedMostRecentFileChanged()
{
  // Move the selected item to the front of the list
  this.MostRecentFiles.Move(this.MostRecentFiles.IndexOf(this.SelectedRecentFile), 0);
}

private string _selectedRecentFile;
public string SelectedRecentFile
{
    get { return _selectedRecentFile; }
    set
    {
        _selectedRecentFile= value;
        OnSelectedMostRecentFileChanged();
        OnPropertyChanged(nameof(SelectedRecentFile));
    }
}

private ObservableCollection<string> _mostRecentFiles = new ObservableCollection<string>();
public ObservableCollection<string> MostRecentFiles
{
    get { return _mostRecentFiles; }
    set
    {
        _mostRecentFiles = value;
        OnPropertyChanged(nameof(MostRecentFiles));
    }
}

看法:

<ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
    <ribbon:RibbonGallery Name="RecentDocuments" CanUserFilter="False" 
        SelectedItem="{Binding SelectedRecentFile}">
        <ribbon:RibbonGalleryCategory Header="Recent Documents"
            ItemsSource="{Binding MostRecentFiles}">
        </ribbon:RibbonGalleryCategory>
    </ribbon:RibbonGallery>
</ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
于 2019-07-20T14:36:35.907 回答