MainWindowViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 MiniMvvm;
  9. using Avalonia.Controls;
  10. using Avalonia.Metadata;
  11. using Avalonia.Controls.Selection;
  12. namespace BindingDemo.ViewModels
  13. {
  14. public class MainWindowViewModel : ViewModelBase
  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<string>>(
  25. Enumerable.Range(0, 20).Select(x => new TestItem<string>
  26. {
  27. Value = "Item " + x,
  28. Detail = "Item " + x + " details",
  29. }));
  30. Selection = new SelectionModel<TestItem<string>> { SingleSelect = false };
  31. ShuffleItems = MiniCommand.Create(() =>
  32. {
  33. var r = new Random();
  34. Items.Move(r.Next(Items.Count), 1);
  35. });
  36. StringValueCommand = MiniCommand.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<string>> Items { get; }
  54. public SelectionModel<TestItem<string>> Selection { get; }
  55. public MiniCommand 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 MiniCommand 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. // Nested class, jsut so we can test it in XAML
  100. public class TestItem<T> : ViewModelBase
  101. {
  102. private T _value;
  103. private string _detail;
  104. public T Value
  105. {
  106. get { return _value; }
  107. set { this.RaiseAndSetIfChanged(ref this._value, value); }
  108. }
  109. public string Detail
  110. {
  111. get { return _detail; }
  112. set { this.RaiseAndSetIfChanged(ref this._detail, value); }
  113. }
  114. }
  115. }
  116. }