Statistiques
| Branche: | Révision:

root / GES_PAC / Controls / DoubleEntry.cs @ 3fef487c

Historique | Voir | Annoter | Télécharger (2,054 ko)

1
using System.Globalization;
2

    
3
namespace GES_PAC.Controls
4
{
5
    public class DoubleEntry : Entry
6
    {
7
        public static readonly BindableProperty DoubleValueProperty =
8
            BindableProperty.Create(
9
                nameof(DoubleValue),
10
                typeof(double?),
11
                typeof(DoubleEntry),
12
                default(double?),
13
                BindingMode.TwoWay,
14
                propertyChanged: OnDoubleValueChanged);
15

    
16
        public double? DoubleValue
17
        {
18
            get => (double?)GetValue(DoubleValueProperty);
19
            set => SetValue(DoubleValueProperty, value);
20
        }
21

    
22
        public DoubleEntry()
23
        {
24
            Keyboard = Keyboard.Numeric;
25
            PlaceholderColor = Colors.LightGray;
26
            TextColor = Colors.Black;
27
            Placeholder = "0,0";
28
            FontSize = 16;
29

    
30
            TextChanged += OnTextChanged;
31
        }
32

    
33
        private static void OnDoubleValueChanged(BindableObject bindable, object oldValue, object newValue)
34
        {
35
            if (bindable is DoubleEntry entry && newValue is double val)
36
            {
37
                var str = val.ToString(CultureInfo.CurrentCulture);
38
                if (entry.Text != str)
39
                    entry.Text = str;
40
            }
41
        }
42

    
43
        private void OnTextChanged(object sender, TextChangedEventArgs e)
44
        {
45
            var input = e.NewTextValue;
46

    
47
            if (string.IsNullOrWhiteSpace(input))
48
                return;
49

    
50
            if (input.StartsWith(","))
51
            {
52
                MainThread.BeginInvokeOnMainThread(() =>
53
                {
54
                    TextChanged -= OnTextChanged;
55
                    Text = "0" + input;
56
                    TextChanged += OnTextChanged;
57
                });
58
                return;
59
            }
60
            if (double.TryParse(input, NumberStyles.Float, CultureInfo.CurrentCulture, out double value))
61
            {
62
                SetValue(DoubleValueProperty, value);
63
            }
64
            else
65
            {
66
                SetValue(DoubleValueProperty, null);
67
            }
68
        }
69
    }
70
}