MainWindowViewModel.cs 3.7 KB

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