1
0

MainWindowViewModel.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }
  47. public ObservableCollection<TestItem> Items { get; }
  48. public ObservableCollection<TestItem> SelectedItems { get; }
  49. public ReactiveCommand<object> ShuffleItems { get; }
  50. public string BooleanString
  51. {
  52. get { return _booleanString; }
  53. set { this.RaiseAndSetIfChanged(ref _booleanString, value); }
  54. }
  55. public double DoubleValue
  56. {
  57. get { return _doubleValue; }
  58. set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
  59. }
  60. public string StringValue
  61. {
  62. get { return _stringValue; }
  63. set { this.RaiseAndSetIfChanged(ref _stringValue, value); }
  64. }
  65. public bool BooleanFlag
  66. {
  67. get { return _booleanFlag; }
  68. set { this.RaiseAndSetIfChanged(ref _booleanFlag, value); }
  69. }
  70. public string CurrentTime
  71. {
  72. get { return _currentTime; }
  73. private set { this.RaiseAndSetIfChanged(ref _currentTime, value); }
  74. }
  75. public ReactiveCommand<object> StringValueCommand { get; }
  76. public DataAnnotationsErrorViewModel DataAnnotationsValidation { get; } = new DataAnnotationsErrorViewModel();
  77. public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
  78. public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
  79. }
  80. }