MainWindowViewModel.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. public MainWindowViewModel()
  18. {
  19. Items = new ObservableCollection<TestItem>(
  20. Enumerable.Range(0, 20).Select(x => new TestItem
  21. {
  22. StringValue = "Item " + x,
  23. Detail = "Item " + x + " details",
  24. }));
  25. SelectedItems = new ObservableCollection<TestItem>();
  26. ShuffleItems = ReactiveCommand.Create();
  27. ShuffleItems.Subscribe(_ =>
  28. {
  29. var r = new Random();
  30. Items.Move(r.Next(Items.Count), 1);
  31. });
  32. StringValueCommand = ReactiveCommand.Create();
  33. StringValueCommand.Subscribe(param =>
  34. {
  35. BooleanFlag = !BooleanFlag;
  36. StringValue = param.ToString();
  37. });
  38. Task.Run(() =>
  39. {
  40. while (true)
  41. {
  42. CurrentTime = DateTimeOffset.Now.ToString();
  43. Thread.Sleep(1000);
  44. }
  45. });
  46. CurrentTimeObservable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
  47. .Select(x => DateTimeOffset.Now.ToString());
  48. }
  49. public ObservableCollection<TestItem> Items { get; }
  50. public ObservableCollection<TestItem> SelectedItems { get; }
  51. public ReactiveCommand<object> ShuffleItems { get; }
  52. public string BooleanString
  53. {
  54. get { return _booleanString; }
  55. set { this.RaiseAndSetIfChanged(ref _booleanString, value); }
  56. }
  57. public double DoubleValue
  58. {
  59. get { return _doubleValue; }
  60. set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
  61. }
  62. public string StringValue
  63. {
  64. get { return _stringValue; }
  65. set { this.RaiseAndSetIfChanged(ref _stringValue, value); }
  66. }
  67. public bool BooleanFlag
  68. {
  69. get { return _booleanFlag; }
  70. set { this.RaiseAndSetIfChanged(ref _booleanFlag, value); }
  71. }
  72. public string CurrentTime
  73. {
  74. get { return _currentTime; }
  75. private set { this.RaiseAndSetIfChanged(ref _currentTime, value); }
  76. }
  77. public IObservable<string> CurrentTimeObservable { get; }
  78. public ReactiveCommand<object> StringValueCommand { get; }
  79. public DataAnnotationsErrorViewModel DataAnnotationsValidation { get; } = new DataAnnotationsErrorViewModel();
  80. public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
  81. public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
  82. }
  83. }