EditorViewModel.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. namespace XamlTestApplication.ViewModels
  2. {
  3. using ReactiveUI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. public class ShellViewModel : ReactiveObject
  11. {
  12. private ShellViewModel()
  13. {
  14. documents = new ObservableCollection<EditorViewModel>();
  15. AddDocumentCommand = ReactiveCommand.Create();
  16. AddDocumentCommand.Subscribe(_ =>
  17. {
  18. Documents.Add(new EditorViewModel());
  19. });
  20. }
  21. public static ShellViewModel Instance = new ShellViewModel();
  22. private ObservableCollection<EditorViewModel> documents;
  23. public ObservableCollection<EditorViewModel> Documents
  24. {
  25. get { return documents; }
  26. set { this.RaiseAndSetIfChanged(ref documents, value); }
  27. }
  28. private EditorViewModel selectedDocument;
  29. public EditorViewModel SelectedDocument
  30. {
  31. get { return selectedDocument; }
  32. set { this.RaiseAndSetIfChanged(ref selectedDocument, value); }
  33. }
  34. public ReactiveCommand<object> AddDocumentCommand { get; }
  35. }
  36. public class EditorViewModel : ReactiveObject
  37. {
  38. public EditorViewModel()
  39. {
  40. text = "This is some text.";
  41. CloseCommand = ReactiveCommand.Create();
  42. CloseCommand.Subscribe(_ =>
  43. {
  44. ShellViewModel.Instance.Documents.Remove(this);
  45. });
  46. }
  47. ~EditorViewModel()
  48. {
  49. }
  50. private string text;
  51. public string Text
  52. {
  53. get { return text; }
  54. set { this.RaiseAndSetIfChanged(ref text, value); }
  55. }
  56. public ReactiveCommand<object> CloseCommand { get; }
  57. }
  58. }