Andy Smith
Andy Smith

Reputation: 13

WPF ObservableCollection not updating in ribbon view

I've created a C# WPF app with a RibbonApplicationMenu displaying a Most Recently Used (MRU) list. Unfortunately the display doesn't update when I select an existing file from the list or upload a new file. In the XAML I have:

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

The DataContext is set to a class containing

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

In the OpenFile routine the code is

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

The MostRecentFiles class contains the main class methods and I've put some sample file paths in the code.

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

Undeleting OnPropertyChanged in UpdateMRUList() produces the error: Error CS1503 Argument 1: cannot convert from 'string' to 'System.ComponentModel.PropertyChangedEventArgs'

When I launch the program the menu correctly displays the three files but when I select one the displayed order doesn't change; I expect the selected file to move to the top of the list. Similarly when I open a new file the filename isn't added to the MRU.

However if I step through the code the lists are being updated in the correct order. What have I done wrong?

Upvotes: 1

Views: 204

Answers (1)

BionicCode
BionicCode

Reputation: 29028

You are binding SelectedValue to a collection. You don't need a custom collection. Just add an ObservableCollection to your view model and move items on selected item changed:

View model:

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

View:

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

Upvotes: 1

Related Questions