63f031924451001f3133b2bcbb5cb0301a3c37ca
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Interactivity / BindingCondition.cs
1 using System;
2 using Tizen.NUI.Xaml;
3
4 namespace Tizen.NUI.Binding
5 {
6     [ProvideCompiled("Tizen.NUI.Xaml.Core.XamlC.PassthroughValueProvider")]
7     [AcceptEmptyServiceProvider]
8     internal sealed class BindingCondition : Condition, IValueProvider
9     {
10         readonly BindableProperty _boundProperty;
11
12         BindingBase _binding;
13         object _triggerValue;
14
15         public BindingCondition()
16         {
17             _boundProperty = BindableProperty.CreateAttached("Bound", typeof(object), typeof(BindingCondition), null, propertyChanged: OnBoundPropertyChanged);
18         }
19
20         public BindingBase Binding
21         {
22             get { return _binding; }
23             set
24             {
25                 if (_binding == value)
26                     return;
27                 if (IsSealed)
28                     throw new InvalidOperationException("Can not change Binding once the Condition has been applied.");
29                 _binding = value;
30             }
31         }
32
33         public object Value
34         {
35             get { return _triggerValue; }
36             set
37             {
38                 if (_triggerValue == value)
39                     return;
40                 if (IsSealed)
41                     throw new InvalidOperationException("Can not change Value once the Condition has been applied.");
42                 _triggerValue = value;
43             }
44         }
45
46         object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
47         {
48             //This is no longer required
49             return this;
50         }
51
52         internal override bool GetState(BindableObject bindable)
53         {
54             object newValue = bindable.GetValue(_boundProperty);
55             return EqualsToValue(newValue);
56         }
57
58         internal override void SetUp(BindableObject bindable)
59         {
60             if (Binding != null)
61                 bindable.SetBinding(_boundProperty, Binding.Clone());
62         }
63
64         internal override void TearDown(BindableObject bindable)
65         {
66             bindable.RemoveBinding(_boundProperty);
67             bindable.ClearValue(_boundProperty);
68         }
69
70         static IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();
71
72         bool EqualsToValue(object other)
73         {
74             if ((other == Value) || (other != null && other.Equals(Value)))
75                 return true;
76
77             object converted = null;
78             if (s_valueConverter != null)
79                 converted = s_valueConverter.Convert(Value, other != null ? other.GetType() : typeof(object), null, null);
80             else
81                 return false;
82
83             return (other == converted) || (other != null && other.Equals(converted));
84         }
85
86         void OnBoundPropertyChanged(BindableObject bindable, object oldValue, object newValue)
87         {
88             bool oldState = EqualsToValue(oldValue);
89             bool newState = EqualsToValue(newValue);
90
91             if (newState == oldState)
92                 return;
93
94             if (ConditionChanged != null)
95                 ConditionChanged(bindable, oldState, newState);
96         }
97     }
98 }