This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
NumericValidationBehavior.cs
122 lines (111 loc) · 4.6 KB
/
NumericValidationBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// NumericValidationBehavior.cs
//
// Author:
// Mark Smith <smmark@microsoft.com>
//
// Copyright (c) 2016-2018 Xamarin, Microsoft.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using Xamarin.Forms;
namespace XamarinUniversity.Infrastructure
{
/// <summary>
/// A custom behavior for the Xamarin.Forms Entry control to
/// restrict the input to be numeric only in the form of a double or integer.
/// </summary>
public class NumericValidationBehavior : Behavior<Entry>
{
#region AllowDecimalProperty
/// <summary>
/// Backing storage for the boolean flag which decides between
/// integer vs. double validation.
/// </summary>
public static BindableProperty AllowDecimalProperty = BindableProperty.Create (
nameof(AllowDecimal),
typeof (bool), typeof (NumericValidationBehavior),
true, BindingMode.OneWay);
/// <summary>
/// Bindable property to hold the boolean flag which decides
/// whether we test for integer vs. double values.
/// </summary>
/// <value>The selected item.</value>
public bool AllowDecimal {
get { return (bool)base.GetValue (AllowDecimalProperty); }
set { base.SetValue (AllowDecimalProperty, value); }
}
#endregion
#region InvalidColorProperty
/// <summary>
/// Backing storage for the color used when the
/// Entry has invalid data (non-numeric).
/// </summary>
public static BindableProperty InvalidColorProperty = BindableProperty.Create (
nameof(InvalidColor),
typeof (Color), typeof (NumericValidationBehavior),
Color.Red, BindingMode.OneWay);
/// <summary>
/// Bindable property to hold the color used when the
/// Entry has invalid data (non-numeric).
/// </summary>
/// <value>The selected item.</value>
public Color InvalidColor {
get { return (Color)GetValue(InvalidColorProperty); }
set { base.SetValue (InvalidColorProperty, value); }
}
#endregion
/// <summary>
/// Called when this behavior is attached to a visual.
/// </summary>
/// <param name="bindable">Visual owner</param>
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.TextChanged += OnEntryTextChanged;
}
/// <summary>
/// Called when this behavior is detached from a visual
/// </summary>
/// <param name="bindable">Visual owner</param>
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.TextChanged -= OnEntryTextChanged;
}
/// <summary>
/// Called when the associated Entry has new text.
/// This changes the text color to reflect whether the data
/// is valid.
/// </summary>
/// <param name="sender">Entry control</param>
/// <param name="args">TextChanged event arguments</param>
void OnEntryTextChanged(object sender, TextChangedEventArgs args)
{
bool isValid = false;
if (AllowDecimal)
{
isValid = double.TryParse(args.NewTextValue, out double result);
}
else
{
isValid = long.TryParse(args.NewTextValue, out long result);
}
((Entry)sender).TextColor = isValid ? Color.Default : InvalidColor;
}
}}