MainWindowViewModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. using ReactiveUI;
  5. using System.Reactive.Linq;
  6. using System.Threading.Tasks;
  7. using System.Threading;
  8. namespace BindingTest.ViewModels
  9. {
  10. public class MainWindowViewModel : ReactiveObject
  11. {
  12. private string _booleanString = "True";
  13. private double _doubleValue = 5.0;
  14. private string _stringValue = "Simple Binding";
  15. private bool _booleanFlag = false;
  16. private string _currentTime;
  17. private NestedCommandViewModel _nested;
  18. public MainWindowViewModel()
  19. {
  20. Items = new ObservableCollection<TestItem>(
  21. Enumerable.Range(0, 20).Select(x => new TestItem
  22. {
  23. StringValue = "Item " + x,
  24. Detail = "Item " + x + " details",
  25. }));
  26. SelectedItems = new ObservableCollection<TestItem>();
  27. ShuffleItems = ReactiveCommand.Create();
  28. ShuffleItems.Subscribe(_ =>
  29. {
  30. var r = new Random();
  31. Items.Move(r.Next(Items.Count), 1);
  32. });
  33. StringValueCommand = ReactiveCommand.Create();
  34. StringValueCommand.Subscribe(param =>
  35. {
  36. BooleanFlag = !BooleanFlag;
  37. StringValue = param.ToString();
  38. NestedModel = _nested ?? new NestedCommandViewModel();
  39. });
  40. Task.Run(() =>
  41. {
  42. while (true)
  43. {
  44. CurrentTime = DateTimeOffset.Now.ToString();
  45. Thread.Sleep(1000);
  46. }
  47. });
  48. CurrentTimeObservable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
  49. .Select(x => DateTimeOffset.Now.ToString());
  50. }
  51. public ObservableCollection<TestItem> Items { get; }
  52. public ObservableCollection<TestItem> SelectedItems { get; }
  53. public ReactiveCommand<object> ShuffleItems { get; }
  54. public string BooleanString
  55. {
  56. get { return _booleanString; }
  57. set { this.RaiseAndSetIfChanged(ref _booleanString, value); }
  58. }
  59. public double DoubleValue
  60. {
  61. get { return _doubleValue; }
  62. set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
  63. }
  64. public string StringValue
  65. {
  66. get { return _stringValue; }
  67. set { this.RaiseAndSetIfChanged(ref _stringValue, value); }
  68. }
  69. public bool BooleanFlag
  70. {
  71. get { return _booleanFlag; }
  72. set { this.RaiseAndSetIfChanged(ref _booleanFlag, value); }
  73. }
  74. public string CurrentTime
  75. {
  76. get { return _currentTime; }
  77. private set { this.RaiseAndSetIfChanged(ref _currentTime, value); }
  78. }
  79. public IObservable<string> CurrentTimeObservable { get; }
  80. public ReactiveCommand<object> StringValueCommand { get; }
  81. public DataAnnotationsErrorViewModel DataAnnotationsValidation { get; } = new DataAnnotationsErrorViewModel();
  82. public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
  83. public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
  84. public NestedCommandViewModel NestedModel
  85. {
  86. get { return _nested; }
  87. private set { this.RaiseAndSetIfChanged(ref _nested, value); }
  88. }
  89. }
  90. }