WPF ObservableCollection 未在功能区视图中更新

WPF ObservableCollection not updating in ribbon view

我创建了一个 C# WPF 应用程序,其中的 RibbonApplicationMenu 显示最近使用 (MRU) 列表。不幸的是,当我 select 列表中的现有文件或上传新文件时,显示不会更新。在 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 设置为 class 包含

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 class 包含主要 class 方法,我在代码中放置了一些示例文件路径。

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 参数 1:无法从 'string' 转换为 'System.ComponentModel.PropertyChangedEventArgs'

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

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

您正在将 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>