12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using ReactiveUI;
- using System;
- using System.ComponentModel;
- using System.Collections;
- namespace BindingDemo.ViewModels
- {
- public class IndeiErrorViewModel : ReactiveObject, INotifyDataErrorInfo
- {
- private int _maximum = 10;
- private int _value;
- private string _valueError;
- public IndeiErrorViewModel()
- {
- this.WhenAnyValue(x => x.Maximum, x => x.Value)
- .Subscribe(_ => UpdateErrors());
- }
- public bool HasErrors
- {
- get { throw new NotImplementedException(); }
- }
- public int Maximum
- {
- get { return _maximum; }
- set { this.RaiseAndSetIfChanged(ref _maximum, value); }
- }
- public int Value
- {
- get { return _value; }
- set { this.RaiseAndSetIfChanged(ref _value, value); }
- }
- public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
- public IEnumerable GetErrors(string propertyName)
- {
- switch (propertyName)
- {
- case nameof(Value):
- return new[] { _valueError };
- default:
- return null;
- }
- }
- private void UpdateErrors()
- {
- if (Value <= Maximum)
- {
- if (_valueError != null)
- {
- _valueError = null;
- ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Value)));
- }
- }
- else
- {
- if (_valueError == null)
- {
- _valueError = "Value must be less than Maximum";
- ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Value)));
- }
- }
- }
- }
- }
|