csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.0 KiB
73 lines
2.0 KiB
// 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 BindingTest.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)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|