猿问

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

我创建了一个 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"); //

    }


在UpdateMRUList()中取消删除OnPropertyChanged会产生错误:错误 CS1503 参数 1:无法从 'string' 转换为 'System.ComponentModel.PropertyChangedEventArgs'


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


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


达令说
浏览 147回答 1
1回答

暮色呼如

您正在绑定SelectedValue到一个集合。您不需要自定义集合。只需添加ObservableCollection到您的视图模型并移动所选项目上的项目已更改:查看型号:private void OnSelectedMostRecentFileChanged(){&nbsp; // Move the selected item to the front of the list&nbsp; this.MostRecentFiles.Move(this.MostRecentFiles.IndexOf(this.SelectedRecentFile), 0);}private string _selectedRecentFile;public string SelectedRecentFile{&nbsp; &nbsp; get { return _selectedRecentFile; }&nbsp; &nbsp; set&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _selectedRecentFile= value;&nbsp; &nbsp; &nbsp; &nbsp; OnSelectedMostRecentFileChanged();&nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(nameof(SelectedRecentFile));&nbsp; &nbsp; }}private ObservableCollection<string> _mostRecentFiles = new ObservableCollection<string>();public ObservableCollection<string> MostRecentFiles{&nbsp; &nbsp; get { return _mostRecentFiles; }&nbsp; &nbsp; set&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _mostRecentFiles = value;&nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged(nameof(MostRecentFiles));&nbsp; &nbsp; }}看法:<ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>&nbsp; &nbsp; <ribbon:RibbonGallery Name="RecentDocuments" CanUserFilter="False"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; SelectedItem="{Binding SelectedRecentFile}">&nbsp; &nbsp; &nbsp; &nbsp; <ribbon:RibbonGalleryCategory Header="Recent Documents"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemsSource="{Binding MostRecentFiles}">&nbsp; &nbsp; &nbsp; &nbsp; </ribbon:RibbonGalleryCategory>&nbsp; &nbsp; </ribbon:RibbonGallery></ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
随时随地看视频慕课网APP
我要回答