-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericEntryBehaviour.cs
More file actions
35 lines (30 loc) · 882 Bytes
/
NumericEntryBehaviour.cs
File metadata and controls
35 lines (30 loc) · 882 Bytes
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
using System;
using Microsoft.Maui.Controls;
namespace JSGradesMini.Behaviors;
public class NumericEntryBehaviour : Behavior<Entry>
{
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.TextChanged += OnEntryTextChanged;
}
protected override void OnDetachingFrom(Entry bindable)
{
base.OnDetachingFrom(bindable);
bindable.TextChanged -= OnEntryTextChanged;
}
private void OnEntryTextChanged(object sender, TextChangedEventArgs e)
{
if (sender is Entry entry)
{
if (!string.IsNullOrEmpty(e.NewTextValue))
{
bool isValid = e.NewTextValue.ToCharArray().All(char.IsDigit);
if (!isValid)
{
entry.Text = e.OldTextValue;
}
}
}
}
}