diff options
Diffstat (limited to 'Xamarin.Forms.Core/Interactivity/MultiCondition.cs')
-rw-r--r-- | Xamarin.Forms.Core/Interactivity/MultiCondition.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Interactivity/MultiCondition.cs b/Xamarin.Forms.Core/Interactivity/MultiCondition.cs new file mode 100644 index 00000000..23ca41c5 --- /dev/null +++ b/Xamarin.Forms.Core/Interactivity/MultiCondition.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; + +namespace Xamarin.Forms +{ + internal sealed class MultiCondition : Condition + { + readonly BindableProperty _aggregatedStateProperty; + + public MultiCondition() + { + _aggregatedStateProperty = BindableProperty.CreateAttached("AggregatedState", typeof(bool), typeof(DataTrigger), false, propertyChanged: OnAggregatedStatePropertyChanged); + Conditions = new TriggerBase.SealedList<Condition>(); + } + + public IList<Condition> Conditions { get; } + + internal override bool GetState(BindableObject bindable) + { + return (bool)bindable.GetValue(_aggregatedStateProperty); + } + + internal override void OnSealed() + { + ((TriggerBase.SealedList<Condition>)Conditions).IsReadOnly = true; + foreach (Condition condition in Conditions) + condition.ConditionChanged = OnConditionChanged; + } + + internal override void SetUp(BindableObject bindable) + { + foreach (Condition condition in Conditions) + condition.SetUp(bindable); + } + + internal override void TearDown(BindableObject bindable) + { + foreach (Condition condition in Conditions) + condition.TearDown(bindable); + } + + void OnAggregatedStatePropertyChanged(BindableObject bindable, object oldValue, object newValue) + { + if ((bool)oldValue == (bool)newValue) + return; + + if (ConditionChanged != null) + ConditionChanged(bindable, (bool)oldValue, (bool)newValue); + } + + void OnConditionChanged(BindableObject bindable, bool oldValue, bool newValue) + { + var oldState = (bool)bindable.GetValue(_aggregatedStateProperty); + var newState = true; + foreach (Condition condition in Conditions) + { + if (!condition.GetState(bindable)) + { + newState = false; + break; + } + } + if (newState != oldState) + bindable.SetValue(_aggregatedStateProperty, newState); + } + } +}
\ No newline at end of file |