I can not find an adequate implementation of the "List of recently opened files" for WPF.

I think the list should be stored in Property.Settings.Default . There you can store a list of type StringCollection .

  • Well, yes, for this settings and invented. What is the question? - VladD
  • I can not tie the command to the menu items created on this list - MaximK
  • Well, Settings is a model object anyway, so you need another VM. - VladD
  • Are you MenuItem s creating manually in code? - Sam

1 answer 1

The list of files we have is stored in the program settings: Property.Settings.Default.RecientlyList

Model

 public class RecentlyFileModel { public string Title { get; set; } public string FileName { get; set; } public ICommand OnClickCommand { get; set; } } 

Viewmodel

 public class RecentlyFiles_VM : PropertyChangedBase { private ObservableCollection<RecentlyFileModel> _recentList = new ObservableCollection<RecentlyFileModel>(); public ObservableCollection<RecentlyFileModel> Items { get { return _recentList; } set { Items = value; RaisePropertyChanged("Items"); } } public RecentlyFiles_VM() { Items.Add(new RecentlyFileModel() { Title = "test.jpg", FileName = @"c:\test.jpg" }); } public RecentlyFiles_VM(StringCollection StringCollection) { foreach (string fName in StringCollection) { Items.Add(new RecentlyFileModel() { Title = Path.GetFileName(fName), FileName = fName }); } } private ICommand _command; public ICommand OnClick { get { return _command; } set { _command = value; foreach (RecentlyFileModel rfm in Items) { rfm.OnClickCommand = _command; } } } } 

View

 public partial class MainWindow : Window { RecentlyFiles_VM _recentlyList; public MainWindow() { InitializeComponent(); // RecentlyFiles_VM _recentlyList = (RecentlyFiles_VM)DataContext; _recentlyList = new RecentlyFiles_VM(Properties.Settings.Default.RecentlyFilesList); this.DataContext = _recentlyList; _recentlyList.OnClick = ParamsCommand; } private RelayCommand<string> _withParamCommand; public ICommand ParamsCommand { get { if (null == _withParamCommand) _withParamCommand = new RelayCommand<string>(ExecuteParameterCommand); return _withParamCommand; } } private void ExecuteParameterCommand(string obj) { //throw new NotImplementedException(); MessageBox.Show(obj); } 

XAML:

 <Menu> <MenuItem Header="File" ItemsSource="{Binding Items}"> <MenuItem.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="Header" Value="{Binding Title}"/> <Setter Property="Command" Value="{Binding OnClickCommand}"/> <Setter Property="CommandParameter" Value="{Binding FileName}"/> </Style> </MenuItem.ItemContainerStyle> </MenuItem> <MenuItem Header="Test" Command="{Binding OpenRecentFile, ElementName=window, Mode=OneWay}" /> <MenuItem Header="CMD With Params" Command="{Binding ParamsCommand, ElementName=window, Mode=OneWay}" CommandParameter="Test"/> </Menu> 

Now we can read the list and create dynamic menus, as well as run the command for execution. It remains to add the ability to add, delete from the list, well, something at your discretion.

Please comment on this approach and correct implementation. Thank.

  • Well, I would have done a RecentlyFileModel immutable (that is, without setters), and so it seems good. - VladD
  • @VladD when do you think the file is added to the list? - MaximK
  • one
    As soon as it was opened (if the discovery was successful). Moreover, if the file was already on the list, then it should in theory fly up. And as it seems to me, you need to immediately save the changes "to disk" (that is, Settings.Default.Save() or there it is). - VladD