我已经创建了一个带有RibbonApplicationMenu的C#WPF应用,该功能显示了最近使用(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参数1:无法从“字符串”转换为“ System.ComponentModel.PropertyChangedEventArgs”
当我启动程序时,菜单会正确显示三个文件,但是当我选择一个文件时,显示的顺序不会改变。我希望所选文件移到列表顶部。同样,当我打开一个新文件时,文件名也不会添加到MRU中。
但是,如果我单步执行代码,则会以正确的顺序更新列表。我做错了什么?
答案 0 :(得分: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>