2 using System.ComponentModel;
3 using System.Reflection;
6 namespace Tizen.NUI.Binding
8 [ProvideCompiled("Tizen.NUI.Core.XamlC.PassthroughValueProvider")]
9 [AcceptEmptyServiceProvider]
10 internal sealed class XamlPropertyCondition : Condition, IValueProvider
12 readonly BindableProperty _stateProperty;
14 BindableProperty _property;
17 public XamlPropertyCondition()
19 _stateProperty = BindableProperty.CreateAttached("State", typeof(bool), typeof(XamlPropertyCondition), false, propertyChanged: OnStatePropertyChanged);
22 public BindableProperty Property
24 get { return _property; }
27 if (_property == value)
30 throw new InvalidOperationException("Can not change Property once the Trigger has been applied.");
34 if (_property != null && s_valueConverter != null)
36 Func<MemberInfo> minforetriever = () => _property.DeclaringType.GetRuntimeProperty(_property.PropertyName);
37 Value = s_valueConverter.Convert(Value, _property.ReturnType, minforetriever, null);
44 get { return _triggerValue; }
47 if (_triggerValue == value)
50 throw new InvalidOperationException("Can not change Value once the Trigger has been applied.");
53 if (_property != null && s_valueConverter != null)
55 Func<MemberInfo> minforetriever = () => _property.DeclaringType.GetRuntimeProperty(_property.PropertyName);
56 _triggerValue = s_valueConverter.Convert(value, _property.ReturnType, minforetriever, null);
60 _triggerValue = value;
66 object IValueProvider.ProvideValue(IServiceProvider serviceProvider)
68 //This is no longer required
72 internal override bool GetState(BindableObject bindable)
74 return (bool)bindable.GetValue(_stateProperty);
77 static IValueConverterProvider s_valueConverter = DependencyService.Get<IValueConverterProvider>();
79 internal override void SetUp(BindableObject bindable)
81 object newvalue = bindable.GetValue(Property);
82 bool newState = (newvalue == Value) || (newvalue != null && newvalue.Equals(Value));
83 bindable.SetValue(_stateProperty, newState);
84 bindable.PropertyChanged += OnAttachedObjectPropertyChanged;
87 internal override void TearDown(BindableObject bindable)
89 bindable.ClearValue(_stateProperty);
90 bindable.PropertyChanged -= OnAttachedObjectPropertyChanged;
93 void OnAttachedObjectPropertyChanged(object sender, PropertyChangedEventArgs e)
95 var bindable = (BindableObject)sender;
96 var oldState = (bool)bindable.GetValue(_stateProperty);
100 if (e.PropertyName != Property.PropertyName)
102 object newvalue = bindable.GetValue(Property);
103 bool newstate = (newvalue == Value) || (newvalue != null && newvalue.Equals(Value));
104 if (oldState != newstate)
105 bindable.SetValue(_stateProperty, newstate);
108 void OnStatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
110 if ((bool)oldValue == (bool)newValue)
113 ConditionChanged?.Invoke(bindable, (bool)oldValue, (bool)newValue);