1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.ObjectModel;
- using System.Linq;
- using ReactiveUI;
- using System.Reactive.Linq;
- using System.Threading.Tasks;
- using System.Threading;
- namespace BindingTest.ViewModels
- {
- public class MainWindowViewModel : ReactiveObject
- {
- private string _booleanString = "True";
- private double _doubleValue = 5.0;
- private string _stringValue = "Simple Binding";
- private bool _booleanFlag = false;
- private string _currentTime;
- public MainWindowViewModel()
- {
- Items = new ObservableCollection<TestItem>(
- Enumerable.Range(0, 20).Select(x => new TestItem
- {
- StringValue = "Item " + x,
- Detail = "Item " + x + " details",
- }));
- SelectedItems = new ObservableCollection<TestItem>();
- ShuffleItems = ReactiveCommand.Create();
- ShuffleItems.Subscribe(_ =>
- {
- var r = new Random();
- Items.Move(r.Next(Items.Count), 1);
- });
- StringValueCommand = ReactiveCommand.Create();
- StringValueCommand.Subscribe(param =>
- {
- BooleanFlag = !BooleanFlag;
- StringValue = param.ToString();
- });
- Task.Run(() =>
- {
- while (true)
- {
- CurrentTime = DateTimeOffset.Now.ToString();
- Thread.Sleep(1000);
- }
- });
- CurrentTimeObservable = Observable.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
- .Select(x => DateTimeOffset.Now.ToString());
- }
- public ObservableCollection<TestItem> Items { get; }
- public ObservableCollection<TestItem> SelectedItems { get; }
- public ReactiveCommand<object> ShuffleItems { get; }
- public string BooleanString
- {
- get { return _booleanString; }
- set { this.RaiseAndSetIfChanged(ref _booleanString, value); }
- }
- public double DoubleValue
- {
- get { return _doubleValue; }
- set { this.RaiseAndSetIfChanged(ref _doubleValue, value); }
- }
- public string StringValue
- {
- get { return _stringValue; }
- set { this.RaiseAndSetIfChanged(ref _stringValue, value); }
- }
- public bool BooleanFlag
- {
- get { return _booleanFlag; }
- set { this.RaiseAndSetIfChanged(ref _booleanFlag, value); }
- }
- public string CurrentTime
- {
- get { return _currentTime; }
- private set { this.RaiseAndSetIfChanged(ref _currentTime, value); }
- }
- public IObservable<string> CurrentTimeObservable { get; }
- public ReactiveCommand<object> StringValueCommand { get; }
- public DataAnnotationsErrorViewModel DataAnnotationsValidation { get; } = new DataAnnotationsErrorViewModel();
- public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
- public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
- }
- }
|