summaryrefslogtreecommitdiff
path: root/product/desktop.ui/views/controls/ObservableProperty.cs
blob: b27116afb99322e101cbdc7935d02cfa5b132299 (plain)
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
using System;
using System.ComponentModel;
using solidware.financials.windows.ui.presenters.validation;

namespace solidware.financials.windows.ui.views.controls
{
    public class ObservableProperty<T> : Observable<T>, IDataErrorInfo
    {
        T value;

        public ObservableProperty() : this(default(T)) {}

        public ObservableProperty(T value)
        {
            this.value = value;
            Notification = new Notification<Observable<T>>();
        }

        public Notification<Observable<T>> Notification { get; private set; }

        public T Value
        {
            get { return value; }
            set
            {
                this.value = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public void WhenChanged(Action observer)
        {
            PropertyChanged += (o, e) => observer();
        }

        public void Register<TSeverity>(Func<T, bool> failCondition, Func<string> errorMessage) where TSeverity : Severity, new()
        {
            Notification.Register<TSeverity>(x => x.Value, () => failCondition(Value), errorMessage);
        }

        object Observable.Value
        {
            get { return value; }
        }

        static public implicit operator T(ObservableProperty<T> val)
        {
            return val.value;
        }

        public event PropertyChangedEventHandler PropertyChanged = (o, e) => {};

        public override string ToString()
        {
            return value.ToString();
        }

        public string this[string property]
        {
            get { return Notification[property]; }
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }
    }
}