Ejemplo n.º 1
0
        private static void SubscribeInternal(IBindingSourceAccessor accessor, EventHandler <IBindingSource, ValueChangedEventArgs> handler, bool subscribe)
        {
            var singleSourceAccessor = accessor as ISingleBindingSourceAccessor;

            if (singleSourceAccessor == null)
            {
                var sources = accessor.Sources;
                if (subscribe)
                {
                    for (int index = 0; index < sources.Count; index++)
                    {
                        sources[index].ValueChanged += handler;
                    }
                }
                else
                {
                    for (int index = 0; index < sources.Count; index++)
                    {
                        sources[index].ValueChanged -= handler;
                    }
                }
            }
            else
            {
                if (subscribe)
                {
                    singleSourceAccessor.Source.ValueChanged += handler;
                }
                else
                {
                    singleSourceAccessor.Source.ValueChanged -= handler;
                }
            }
        }
Ejemplo n.º 2
0
 public DataBinding([NotNull] ISingleBindingSourceAccessor target, [NotNull] IBindingSourceAccessor source)
 {
     Should.NotBeNull(target, "target");
     Should.NotBeNull(source, "source");
     _targetAccessor = target;
     _sourceAccessor = source;
     _items          = Empty.Array <IBindingBehavior>();
 }
Ejemplo n.º 3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DataBinding" /> class.
 /// </summary>
 public DataBinding([NotNull] ISingleBindingSourceAccessor target, [NotNull] IBindingSourceAccessor source)
 {
     Should.NotBeNull(target, "target");
     Should.NotBeNull(source, "source");
     _targetAccessor = target;
     _sourceAccessor = source;
     _behaviors      = new BehaviorCollection(this);
 }
Ejemplo n.º 4
0
 protected virtual DataBinding CreateDataBinding(ISingleBindingSourceAccessor target,
                                                 IBindingSourceAccessor source, IBindingManager manager = null)
 {
     if (manager != null)
     {
         BindingServiceProvider.BindingManager = manager;
     }
     return(new DataBinding(target, source));
 }
Ejemplo n.º 5
0
 private void OnValueChanging(IBindingSourceAccessor sender, ValueAccessorChangingEventArgs args)
 {
     if (args.Cancel || _isUpdating)
     {
         return;
     }
     args.Cancel = true;
     _timer.Change(Delay, Timeout.Infinite);
     _context = SynchronizationContext.Current ?? ToolkitServiceProvider.UiSynchronizationContext;
 }
Ejemplo n.º 6
0
 private void OnValueChanging(IBindingSourceAccessor sender, ValueAccessorChangingEventArgs args)
 {
     if (args.Cancel || _isUpdating)
     {
         return;
     }
     args.Cancel = true;
     _timer.Change(Delay, int.MaxValue);
     _context = SynchronizationContext.Current;
 }
Ejemplo n.º 7
0
 public bool SetValue(IBindingSourceAccessor targetAccessor, IDataContext context, bool throwOnError)
 {
     try
     {
         return(SetValueInternal(targetAccessor, context, throwOnError));
     }
     catch (Exception) when(!throwOnError)
     {
         return(false);
     }
 }
Ejemplo n.º 8
0
        private void SourceOnValueChanging(IBindingSourceAccessor sender, ValueAccessorChangingEventArgs args)
        {
            if (args.Cancel)
            {
                return;
            }
            object value = Binding.TargetAccessor.Source.GetPathMembers(false).PenultimateValue;

            if (value != null && !value.IsUnsetValue())
            {
                args.Cancel = (bool)_member.GetValue(value, null);
            }
        }
        private bool MemberNameEqual(string memberName, IBindingSourceAccessor accessor)
        {
            if (string.IsNullOrEmpty(memberName))
            {
                return(true);
            }
            var  paths    = ErrorPaths;
            bool hasPaths = paths != null && paths.Length != 0;

            if (hasPaths)
            {
                for (int i = 0; i < paths.Length; i++)
                {
                    if (ToolkitExtensions.MemberNameEqual(memberName, paths[i], true))
                    {
                        return(true);
                    }
                }
            }
            else if (IsDirectSource)
            {
                return(true);
            }

            var    singleAccessor = accessor as ISingleBindingSourceAccessor;
            string path;

            if (singleAccessor != null)
            {
                path = singleAccessor.Source.Path.Parts.LastOrDefault();
                if (hasPaths && string.IsNullOrEmpty(path))
                {
                    return(false);
                }
                return(ToolkitExtensions.MemberNameEqual(memberName, path, true));
            }
            for (int i = 0; i < accessor.Sources.Count; i++)
            {
                path = accessor.Sources[i].Path.Parts.LastOrDefault();
                if (hasPaths && string.IsNullOrEmpty(path))
                {
                    continue;
                }
                if (ToolkitExtensions.MemberNameEqual(memberName, path, true))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 10
0
        public static bool IsAllMembersAvailable(this IBindingSourceAccessor accessor, bool checkLastMember = false)
        {
            Should.NotBeNull(accessor, nameof(accessor));
            var s = accessor as ISingleBindingSourceAccessor;

            if (s != null)
            {
                return(s.Source.IsAllMembersAvailable(checkLastMember));
            }
            var sources = accessor.Sources;

            for (int i = 0; i < sources.Count; i++)
            {
                if (!sources[i].IsAllMembersAvailable(checkLastMember))
                {
                    return(false);
                }
            }
            return(true);
        }
 /// <summary>
 ///     Sets the source value.
 /// </summary>
 /// <param name="targetAccessor">The specified accessor to get value.</param>
 /// <param name="context">The specified operation context.</param>
 /// <param name="throwOnError">
 ///     true to throw an exception if the value cannot be set.
 /// </param>
 public bool SetValue(IBindingSourceAccessor targetAccessor, IDataContext context, bool throwOnError)
 {
     try
     {
         return SetValueInternal(targetAccessor, context, throwOnError);
     }
     catch (Exception)
     {
         if (throwOnError)
             throw;
         return false;
     }
 }
Ejemplo n.º 12
0
 private static void SubscribeInternal(IBindingSourceAccessor accessor, EventHandler<IBindingSource, ValueChangedEventArgs> handler, bool subscribe)
 {
     var singleSourceAccessor = accessor as ISingleBindingSourceAccessor;
     if (singleSourceAccessor == null)
     {
         var sources = accessor.Sources;
         if (subscribe)
         {
             for (int index = 0; index < sources.Count; index++)
                 sources[index].ValueChanged += handler;
         }
         else
         {
             for (int index = 0; index < sources.Count; index++)
                 sources[index].ValueChanged -= handler;
         }
     }
     else
     {
         if (subscribe)
             singleSourceAccessor.Source.ValueChanged += handler;
         else
             singleSourceAccessor.Source.ValueChanged -= handler;
     }
 }
Ejemplo n.º 13
0
 protected virtual DataBinding CreateDataBinding(ISingleBindingSourceAccessor target,
     IBindingSourceAccessor source, IBindingManager manager = null)
 {
     if (manager != null)
         BindingServiceProvider.BindingManager = manager;
     return new DataBinding(target, source);
 }
 bool IBindingSourceAccessor.SetValue(IBindingSourceAccessor targetAccessor, IDataContext context,
     bool throwOnError)
 {
     return SetValue(targetAccessor, context, throwOnError);
 }
 protected override bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
     bool throwOnError)
 {
     //NOTE By default multibinding doesn't support update source operation.
     return false;
 }
Ejemplo n.º 16
0
 /// <summary>
 ///     Sets the source value.
 /// </summary>
 /// <param name="targetAccessor">The specified accessor to get value.</param>
 /// <param name="context">The specified operation context.</param>
 /// <param name="throwOnError">
 ///     true to throw an exception if the value cannot be set.
 /// </param>
 bool IBindingSourceAccessor.SetValue(IBindingSourceAccessor targetAccessor, IDataContext context,
                                      bool throwOnError)
 {
     return(SetValue(targetAccessor, context, throwOnError));
 }
        /// <summary>
        ///     Sets the source value.
        /// </summary>
        /// <param name="targetAccessor">The specified accessor to get value.</param>
        /// <param name="context">The specified operation context.</param>
        /// <param name="throwOnError">
        ///     true to throw an exception if the value cannot be set.
        /// </param>
        protected override bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
            bool throwOnError)
        {
            IBindingPathMembers members = _bindingSource.GetPathMembers(throwOnError);
            if (!members.AllMembersAvailable)
                return false;

            object penultimateValue = members.PenultimateValue;
            IBindingMemberInfo lastMember = members.LastMember;

            object oldValue;
            object newValue = targetAccessor.GetValue(lastMember, context, throwOnError);
            if (lastMember.CanRead)
            {
                oldValue = lastMember.GetValue(penultimateValue, null);
                if (ReferenceEquals(oldValue, newValue) || newValue.IsUnsetValueOrDoNothing())
                    return false;
            }
            else
            {
                oldValue = BindingConstants.UnsetValue;
                if (newValue.IsUnsetValueOrDoNothing())
                    return false;
            }

            ValueAccessorChangingEventArgs args = null;
            if (ValueChanging != null)
            {
                args = RaiseValueChanging(context, penultimateValue, lastMember, oldValue, newValue);
                if (args != null)
                {
                    if (args.Cancel)
                        return false;
                    if (!ReferenceEquals(newValue, args.NewValue))
                    {
                        newValue = args.NewValue;
                        if (newValue.IsUnsetValueOrDoNothing())
                            return false;
                    }
                }
            }
            newValue = BindingServiceProvider.ValueConverter(lastMember, lastMember.Type, newValue);
            if (Equals(oldValue, newValue))
                return false;
            if (BindingMemberType.Event.Equals(lastMember.MemberType))
            {
                TryRegisterEvent((BindingActionValue)oldValue, newValue, context);
                RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
            }
            else
            {
                if (_closure != null)
                    _closure.Unsubscribe(false);
                lastMember.SetValue(penultimateValue, new[] { newValue });
                if (ValueChanged != null)
                    RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
            }
            return true;
        }
 protected abstract bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
                                          bool throwOnError);
Ejemplo n.º 19
0
 protected override bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
                                          bool throwOnError)
 {
     //NOTE By default multibinding doesn't support update source operation.
     return(false);
 }
 private void SourceOnValueChanging(IBindingSourceAccessor sender, ValueAccessorChangingEventArgs args)
 {
     if (args.Cancel)
         return;
     object value = Binding.TargetAccessor.Source.GetPathMembers(false).PenultimateValue;
     if (value != null && !value.IsUnsetValue())
         args.Cancel = (bool)_member.GetValue(value, null);
 }
        protected override bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
                                                 bool throwOnError)
        {
            IBindingPathMembers members = _bindingSource.GetPathMembers(throwOnError);

            if (!members.AllMembersAvailable)
            {
                return(false);
            }

            object             penultimateValue = members.PenultimateValue;
            IBindingMemberInfo lastMember       = members.LastMember;

            object oldValue;
            object newValue = targetAccessor.GetValue(lastMember, context, throwOnError);

            if (lastMember.CanRead && !BindingMemberType.BindingContext.EqualsWithoutNullCheck(lastMember.MemberType))
            {
                oldValue = lastMember.GetValue(penultimateValue, null);
                if (ReferenceEquals(oldValue, newValue) || newValue.IsUnsetValueOrDoNothing())
                {
                    return(false);
                }
            }
            else
            {
                oldValue = BindingConstants.UnsetValue;
                if (newValue.IsUnsetValueOrDoNothing())
                {
                    return(false);
                }
            }

            ValueAccessorChangingEventArgs args = null;

            if (ValueChanging != null)
            {
                args = RaiseValueChanging(context, penultimateValue, lastMember, oldValue, newValue);
                if (args != null)
                {
                    if (args.Cancel)
                    {
                        return(false);
                    }
                    if (!ReferenceEquals(newValue, args.NewValue))
                    {
                        newValue = args.NewValue;
                        if (newValue.IsUnsetValueOrDoNothing())
                        {
                            return(false);
                        }
                    }
                }
            }
            newValue = BindingServiceProvider.ValueConverter(lastMember, lastMember.Type, newValue);
            if (Equals(oldValue, newValue))
            {
                return(false);
            }
            if (BindingMemberType.Event.EqualsWithoutNullCheck(lastMember.MemberType))
            {
                TryRegisterEvent((BindingActionValue)oldValue, newValue, context);
                RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
            }
            else
            {
                if (_closure != null)
                {
                    _closure.Unsubscribe(false, _isOneTime);
                }
                lastMember.SetSingleValue(penultimateValue, newValue);
                if (ValueChanged != null)
                {
                    RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
                }
            }
            return(true);
        }
 /// <summary>
 ///     Sets the source value.
 /// </summary>
 /// <param name="targetAccessor">The specified accessor to get value.</param>
 /// <param name="context">The specified operation context.</param>
 /// <param name="throwOnError">
 ///     true to throw an exception if the value cannot be set.
 /// </param>
 protected abstract bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
     bool throwOnError);
        protected override bool SetValueInternal(IBindingSourceAccessor targetAccessor, IDataContext context,
                                                 bool throwOnError)
        {
            IBindingPathMembers members = _bindingSource.GetPathMembers(throwOnError);
            object penultimateValue     = members.PenultimateValue;

            if (penultimateValue.IsUnsetValue() || (penultimateValue == null && !members.AllMembersAvailable))
            {
                if (members.Path.IsDebuggable)
                {
                    DebugInfo($"Binding cannot set value for path {members.Path.Path}", new object[] { members });
                }
                return(false);
            }

            IBindingMemberInfo lastMember = members.LastMember;
            object             oldValue;
            object             newValue = targetAccessor.GetValue(lastMember, context, throwOnError);

            if (lastMember.CanRead && !BindingMemberType.BindingContext.EqualsWithoutNullCheck(lastMember.MemberType))
            {
                if (_disableEqualityChecking && !BindingMemberType.Event.EqualsWithoutNullCheck(lastMember.MemberType))
                {
                    oldValue = BindingConstants.UnsetValue;
                }
                else
                {
                    oldValue = lastMember.GetValue(penultimateValue, null);
                }
                if (ReferenceEquals(oldValue, newValue) || newValue.IsUnsetValueOrDoNothing())
                {
                    if (members.Path.IsDebuggable)
                    {
                        DebugInfo($"Binding ignores setter because old value: '{oldValue}' equals to new value '{newValue}'", new[] { members, oldValue, newValue });
                    }
                    return(false);
                }
            }
            else
            {
                oldValue = BindingConstants.UnsetValue;
                if (newValue.IsUnsetValueOrDoNothing())
                {
                    if (members.Path.IsDebuggable)
                    {
                        DebugInfo($"Binding ignores setter for value '{newValue}'", new[] { members, newValue });
                    }
                    return(false);
                }
            }

            ValueAccessorChangingEventArgs args = null;

            if (ValueChanging != null)
            {
                args = RaiseValueChanging(context, penultimateValue, lastMember, oldValue, newValue);
                if (args != null)
                {
                    if (args.Cancel)
                    {
                        return(false);
                    }
                    if (!ReferenceEquals(newValue, args.NewValue))
                    {
                        newValue = args.NewValue;
                        if (newValue.IsUnsetValueOrDoNothing())
                        {
                            if (members.Path.IsDebuggable)
                            {
                                DebugInfo($"Binding ignores setter for value '{newValue}'", new[] { members, newValue });
                            }
                            return(false);
                        }
                    }
                }
            }
            newValue = BindingServiceProvider.ValueConverter(lastMember, lastMember.Type, newValue);
            if (Equals(oldValue, newValue))
            {
                if (members.Path.IsDebuggable)
                {
                    DebugInfo($"Binding ignores setter because old value: '{oldValue}' equals to new value '{newValue}'", new[] { members, oldValue, newValue });
                }
                return(false);
            }
            if (BindingMemberType.Event.EqualsWithoutNullCheck(lastMember.MemberType))
            {
                TryRegisterEvent((BindingActionValue)oldValue, newValue, context, members);
                RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
            }
            else
            {
                _closure?.Unsubscribe(false, _isOneTime);
                lastMember.SetSingleValue(penultimateValue, newValue);
                if (members.Path.IsDebuggable)
                {
                    DebugInfo($"Binding set value: '{newValue}' for source: '{penultimateValue}' with path: '{lastMember.Path}'", new[] { newValue, penultimateValue, lastMember });
                }
                if (ValueChanged != null)
                {
                    RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
                }
            }
            return(true);
        }
 private bool MemberNameEqual(string memberName, IBindingSourceAccessor accessor)
 {
     if (string.IsNullOrEmpty(memberName))
         return true;
     var paths = ErrorPaths;
     bool hasPaths = paths != null && paths.Length != 0;
     if (hasPaths)
     {
         for (int i = 0; i < paths.Length; i++)
         {
             if (ToolkitExtensions.MemberNameEqual(memberName, paths[i], true))
                 return true;
         }
     }
     var singleAccessor = accessor as ISingleBindingSourceAccessor;
     string path;
     if (singleAccessor != null)
     {
         path = singleAccessor.Source.Path.Parts.LastOrDefault();
         if (hasPaths && string.IsNullOrEmpty(path))
             return false;
         return ToolkitExtensions.MemberNameEqual(memberName, path, true);
     }
     for (int i = 0; i < accessor.Sources.Count; i++)
     {
         path = accessor.Sources[i].Path.Parts.LastOrDefault();
         if (hasPaths && string.IsNullOrEmpty(path))
             continue;
         if (ToolkitExtensions.MemberNameEqual(memberName, path, true))
             return true;
     }
     return false;
 }