diff --git a/samples/BindingTest/BindingTest.csproj b/samples/BindingTest/BindingTest.csproj index 441802e396..ac783d1471 100644 --- a/samples/BindingTest/BindingTest.csproj +++ b/samples/BindingTest/BindingTest.csproj @@ -80,7 +80,8 @@ TestItemView.xaml - + + diff --git a/samples/BindingTest/MainWindow.xaml b/samples/BindingTest/MainWindow.xaml index e1aafde5b3..614e3b80c7 100644 --- a/samples/BindingTest/MainWindow.xaml +++ b/samples/BindingTest/MainWindow.xaml @@ -70,10 +70,15 @@ - + + + + + + diff --git a/samples/BindingTest/ViewModels/ExceptionPropertyErrorViewModel.cs b/samples/BindingTest/ViewModels/ExceptionErrorViewModel.cs similarity index 91% rename from samples/BindingTest/ViewModels/ExceptionPropertyErrorViewModel.cs rename to samples/BindingTest/ViewModels/ExceptionErrorViewModel.cs index be34c65c40..e6071e0678 100644 --- a/samples/BindingTest/ViewModels/ExceptionPropertyErrorViewModel.cs +++ b/samples/BindingTest/ViewModels/ExceptionErrorViewModel.cs @@ -6,7 +6,7 @@ using System; namespace BindingTest.ViewModels { - public class ExceptionPropertyErrorViewModel : ReactiveObject + public class ExceptionErrorViewModel : ReactiveObject { private int _lessThan10; diff --git a/samples/BindingTest/ViewModels/IndeiErrorViewModel.cs b/samples/BindingTest/ViewModels/IndeiErrorViewModel.cs new file mode 100644 index 0000000000..b4bb528abb --- /dev/null +++ b/samples/BindingTest/ViewModels/IndeiErrorViewModel.cs @@ -0,0 +1,73 @@ +// 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 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))); + } + } + } + } +} diff --git a/samples/BindingTest/ViewModels/MainWindowViewModel.cs b/samples/BindingTest/ViewModels/MainWindowViewModel.cs index 6fbfb8a23f..e38d19612e 100644 --- a/samples/BindingTest/ViewModels/MainWindowViewModel.cs +++ b/samples/BindingTest/ViewModels/MainWindowViewModel.cs @@ -69,7 +69,7 @@ namespace BindingTest.ViewModels public ReactiveCommand StringValueCommand { get; } - public ExceptionPropertyErrorViewModel ExceptionPropertyValidation { get; } - = new ExceptionPropertyErrorViewModel(); + public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel(); + public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel(); } }