-/*
- * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+/*
+ * Copyright(c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly BindableProperty BindingContextProperty =
- BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
- {
- var bindableObject = (BindableObject)bindable;
- if (newValue != null)
- {
- bindableObject.bindingContext = newValue;
- bindableObject.FlushBinding();
- }
- }),
- defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
- {
- if (null != bindable.bindingContext)
- {
- return bindable.bindingContext;
- }
-
- if (bindable is Container container)
- {
- return container.Parent?.BindingContext;
- }
- else
- {
- return null;
- }
- }));
+ BindableProperty.Create(nameof(BindingContext), typeof(object), typeof(BindableObject), default(object), BindingMode.OneWay, null, BindingContextPropertyChanged,
+ null, null, BindingContextPropertyBindingChanging);
readonly List<BindablePropertyContext> properties = new List<BindablePropertyContext>(4);
set { SetValue(BindingContextProperty, value); }
}
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public int LineNumber { get; set; } = -1;
+
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public int LinePosition { get; set; } = -1;
+
void IDynamicResourceHandler.SetDynamicResource(BindableProperty property, string key)
{
SetDynamicResource(property, key, false);
SetBinding(targetProperty, binding, false);
}
- private bool isCreateByXaml = false;
/// Only used by the IL of xaml, will never changed to not hidden.
[EditorBrowsable(EditorBrowsableState.Never)]
- public virtual bool IsCreateByXaml
+ public bool IsCreateByXaml
{
- get
- {
- return isCreateByXaml;
- }
- set
+ get;
+ set;
+ }
+
+ /// This will be public opened in next ACR.
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ public void ForceNotifyBindedInstance(BindableProperty property)
+ {
+ if (null != property)
{
- isCreateByXaml = value;
+ BindablePropertyContext context = GetOrCreateContext(property);
+ BindingBase binding = context.Binding;
+
+ var currentlyApplying = applying;
+
+ if (binding != null && !currentlyApplying)
+ {
+ applying = true;
+ binding.Apply(true);
+ applying = false;
+ }
+
+ OnPropertyChanged(property.PropertyName);
+
+ PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
+
+ if (null != propertyGroup)
+ {
+ foreach (var relativeProperty in propertyGroup)
+ {
+ if (relativeProperty != property)
+ {
+ var relativeContext = GetOrCreateContext(relativeProperty);
+ var relativeBinding = relativeContext.Binding;
+
+ if (null != relativeBinding)
+ {
+ applying = true;
+ relativeBinding.Apply(true);
+ applying = false;
+ }
+
+ OnPropertyChanged(relativeProperty.PropertyName);
+ }
+ }
+ }
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetValue(BindableProperty property, object value)
{
- if (true == isCreateByXaml)
+ if (true == IsBinded)
{
SetValue(property, value, false, true);
}
{
throw new ArgumentNullException(nameof(property));
}
- property.PropertyChanged?.Invoke(this, null, value);
-
- OnPropertyChanged(property.PropertyName);
- OnPropertyChangedWithData(property);
- }
- }
- internal void SetValueAndForceSendChangeSignal(BindableProperty property, object value)
- {
- if (property == null)
- throw new ArgumentNullException(nameof(property));
+ object oldvalue = null;
+ if (null == property.DefaultValueCreator)
+ {
+ BindablePropertyContext context = GetOrCreateContext(property);
+ if (null != context)
+ {
+ oldvalue = context.Value;
+ context.Value = value;
+ }
+ }
+ else
+ {
+ oldvalue = property.DefaultValueCreator.Invoke(this);
+ }
- if (true == isCreateByXaml)
- {
- if (property.IsReadOnly)
- throw new InvalidOperationException(string.Format("The BindableProperty \"{0}\" is readonly.", property.PropertyName));
+ property.PropertyChanged?.Invoke(this, oldvalue, value);
- SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
- SetValuePrivateFlags.ManuallySet | SetValuePrivateFlags.CheckAccess, true);
- }
- else
- {
- property.PropertyChanged?.Invoke(this, null, value);
+ OnPropertyChanged(property.PropertyName);
+ OnPropertyChangedWithData(property);
}
}
{
PropertyToGroup.Add(property, group);
}
+
+ if (null != group && !(group.Contains(property)))
+ {
+ group.Add(property);
+ }
}
/// <summary>
/// Apply the bindings to BindingContext.
if (fromStyle && !CanBeSetFromStyle(targetProperty))
return;
- IsCreateByXaml = true;
+ IsBinded = true;
var context = GetOrCreateContext(targetProperty);
if (fromStyle)
internal void SetValueCore(BindablePropertyKey propertyKey, object value, SetValueFlags attributes = SetValueFlags.None)
{
- SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None, false);
+ SetValueCore(propertyKey.BindableProperty, value, attributes, SetValuePrivateFlags.None);
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes = SetValueFlags.None)
{
- SetValueCore(property, value, attributes, SetValuePrivateFlags.Default, false);
+ SetValueCore(property, value, attributes, SetValuePrivateFlags.Default);
}
- internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, bool forceSendChangeSignal)
+ internal void SetValueCore(BindableProperty property, object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
{
bool checkAccess = (privateAttributes & SetValuePrivateFlags.CheckAccess) != 0;
bool manuallySet = (privateAttributes & SetValuePrivateFlags.ManuallySet) != 0;
else
{
context.Attributes |= BindableContextAttributes.IsBeingSet;
- SetValueActual(property, context, value, currentlyApplying, forceSendChangeSignal, attributes, silent);
+ SetValueActual(property, context, value, currentlyApplying, attributes, silent);
Queue<SetValueArgs> delayQueue = context.DelayedSetters;
if (delayQueue != null)
while (delayQueue.Count > 0)
{
SetValueArgs s = delayQueue.Dequeue();
- SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, forceSendChangeSignal, s.Attributes);
+ SetValueActual(s.Property, s.Context, s.Value, s.CurrentlyApplying, s.Attributes);
}
context.DelayedSetters = null;
}
}
+ internal bool IsBinded
+ {
+ get;
+ set;
+ } = false;
+
+ static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
+ {
+ bindable.inheritedContext = null;
+ bindable.ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
+ bindable.OnBindingContextChanged();
+
+ if (newvalue is BindableObject targetBindableObject)
+ {
+ targetBindableObject.IsBinded = true;
+ }
+ }
+
static void BindingContextPropertyBindingChanging(BindableObject bindable, BindingBase oldBindingBase, BindingBase newBindingBase)
{
object context = bindable.inheritedContext;
context.Binding = null;
}
- void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
+ internal void SetValue(BindableProperty property, object value, bool fromStyle, bool checkAccess)
{
if (property == null)
throw new ArgumentNullException(nameof(property));
return;
SetValueCore(property, value, SetValueFlags.ClearOneWayBindings | SetValueFlags.ClearDynamicResource,
- (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0),
- false);
+ (fromStyle ? SetValuePrivateFlags.FromStyle : SetValuePrivateFlags.ManuallySet) | (checkAccess ? SetValuePrivateFlags.CheckAccess : 0));
}
- void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, bool forceSendChangeSignal, SetValueFlags attributes, bool silent = false)
+ void SetValueActual(BindableProperty property, BindablePropertyContext context, object value, bool currentlyApplying, SetValueFlags attributes, bool silent = false)
{
object original = context.Value;
bool raiseOnEqual = (attributes & SetValueFlags.RaiseOnEqual) != 0;
PropertyToGroup.TryGetValue(property, out HashSet<BindableProperty> propertyGroup);
- if (!silent)
+ if (!silent && (!same || raiseOnEqual))
{
- if ((!same || raiseOnEqual))
- {
- property.PropertyChanged?.Invoke(this, original, value);
-
- if (binding != null && !currentlyApplying)
- {
- applying = true;
- binding.Apply(true);
- applying = false;
- }
-
- OnPropertyChanged(property.PropertyName);
-
- if (null != propertyGroup)
- {
- foreach (var relativeProperty in propertyGroup)
- {
- if (relativeProperty != property)
- {
- var relativeContext = GetOrCreateContext(relativeProperty);
- var relativeBinding = relativeContext.Binding;
-
- if (null != relativeBinding)
- {
- applying = true;
- relativeBinding.Apply(true);
- applying = false;
- }
+ property.PropertyChanged?.Invoke(this, original, value);
- OnPropertyChanged(relativeProperty.PropertyName);
- }
- }
- }
- }
- else if (true == same && true == forceSendChangeSignal)
+ if (binding != null && !currentlyApplying)
{
- if (binding != null && !currentlyApplying)
- {
- applying = true;
- binding.Apply(true);
- applying = false;
- }
+ applying = true;
+ binding.Apply(true);
+ applying = false;
+ }
- OnPropertyChanged(property.PropertyName);
+ OnPropertyChanged(property.PropertyName);
- if (null != propertyGroup)
+ if (null != propertyGroup)
+ {
+ foreach (var relativeProperty in propertyGroup)
{
- foreach (var relativeProperty in propertyGroup)
+ if (relativeProperty != property)
{
- if (relativeProperty != property)
- {
- var relativeContext = GetOrCreateContext(relativeProperty);
- var relativeBinding = relativeContext.Binding;
-
- if (null != relativeBinding)
- {
- applying = true;
- relativeBinding.Apply(true);
- applying = false;
- }
+ var relativeContext = GetOrCreateContext(relativeProperty);
+ var relativeBinding = relativeContext.Binding;
- OnPropertyChanged(relativeProperty.PropertyName);
+ if (null != relativeBinding)
+ {
+ applying = true;
+ relativeBinding.Apply(true);
+ applying = false;
}
+
+ OnPropertyChanged(relativeProperty.PropertyName);
}
}
}
Attributes = attributes;
}
}
-
- internal void AddChildBindableObject(BindableObject child)
- {
- if (null != child)
- {
- children.Add(child);
- child.FlushBinding();
- }
- }
-
- internal void RemoveChildBindableObject(BindableObject child)
- {
- children.Remove(child);
- }
-
- private List<BindableObject> children = new List<BindableObject>();
-
- private void FlushBinding()
- {
- ApplyBindings(skipBindingContext: true, fromBindingContextChanged: true);
- OnBindingContextChanged();
-
- foreach (var child in children)
- {
- child.FlushBinding();
- }
- }
}
}