Example #1
0
        public void RaiseValueChanging(ValueAccessorChangingEventArgs e)
        {
            var handler = ValueChanging;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Example #2
0
 private void OnValueChanging(IBindingSourceAccessor sender, ValueAccessorChangingEventArgs args)
 {
     if (args.Cancel || _isUpdating)
     {
         return;
     }
     args.Cancel = true;
     _timer.Change(Delay, int.MaxValue);
     _context = SynchronizationContext.Current;
 }
Example #3
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;
 }
Example #4
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 ValueAccessorChangingEventArgs RaiseValueChanging(IDataContext context, object penultimateValue,
                                                                  IBindingMemberInfo lastMember, object oldValue, object newValue)
        {
            var valueChanging = ValueChanging;

            if (valueChanging == null)
            {
                return(null);
            }
            var args = new ValueAccessorChangingEventArgs(context, penultimateValue, lastMember, oldValue, newValue);

            valueChanging(this, args);
            return(args);
        }
        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);
        }
        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);
        }