Browse Source

Added INDEI validation to BindingTest.

Steven Kirk 9 years ago
parent
commit
ab515ee513

+ 2 - 1
samples/BindingTest/BindingTest.csproj

@@ -80,7 +80,8 @@
     <Compile Include="TestItemView.xaml.cs">
       <DependentUpon>TestItemView.xaml</DependentUpon>
     </Compile>
-    <Compile Include="ViewModels\ExceptionPropertyErrorViewModel.cs" />
+    <Compile Include="ViewModels\IndeiErrorViewModel.cs" />
+    <Compile Include="ViewModels\ExceptionErrorViewModel.cs" />
     <Compile Include="ViewModels\MainWindowViewModel.cs" />
     <Compile Include="ViewModels\TestItem.cs" />
   </ItemGroup>

+ 6 - 1
samples/BindingTest/MainWindow.xaml

@@ -70,10 +70,15 @@
     </TabItem>
     <TabItem Header="Property Validation">
       <StackPanel Orientation="Horizontal">
-        <StackPanel Margin="18" Gap="4" Width="200" DataContext="{Binding ExceptionPropertyValidation}">
+        <StackPanel Margin="18" Gap="4" MinWidth="200" DataContext="{Binding ExceptionDataValidation}">
           <TextBlock FontSize="16" Text="Exception Validation"/>
           <TextBox Watermark="Less Than 10" UseFloatingWatermark="True" Text="{Binding Path=LessThan10}"/>
         </StackPanel>
+        <StackPanel Margin="18" Gap="4" MinWidth="200" DataContext="{Binding IndeiDataValidation}">
+          <TextBlock FontSize="16" Text="INotifyDataErrorInfo Validation"/>
+          <TextBox Watermark="Maximum" UseFloatingWatermark="True" Text="{Binding Path=Maximum}"/>
+          <TextBox Watermark="Value" UseFloatingWatermark="True" Text="{Binding Path=Value}"/>
+        </StackPanel>
       </StackPanel>
     </TabItem>
     <TabItem Header="Commands">

+ 1 - 1
samples/BindingTest/ViewModels/ExceptionPropertyErrorViewModel.cs → 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;
 

+ 73 - 0
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<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)));
+                }
+            }
+        }
+    }
+}

+ 2 - 2
samples/BindingTest/ViewModels/MainWindowViewModel.cs

@@ -69,7 +69,7 @@ namespace BindingTest.ViewModels
 
         public ReactiveCommand<object> StringValueCommand { get; }
 
-        public ExceptionPropertyErrorViewModel ExceptionPropertyValidation { get; }
-            = new ExceptionPropertyErrorViewModel();
+        public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
+        public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
     }
 }