public override void Dispose()
 {
     _closure?.Unsubscribe(true, _isOneTime);
     _bindingSource.Dispose();
     ValueChanging = null;
     ValueChanged  = null;
     _closure      = null;
     base.Dispose();
 }
Пример #2
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>
        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);
                        }
                    }
                }
            }
            if (AutoConvertValue)
            {
                newValue = BindingServiceProvider.ValueConverter(lastMember.Type, newValue);
            }
            if (Equals(oldValue, newValue))
            {
                return(false);
            }
            if (lastMember.MemberType == BindingMemberType.Event)
            {
                TryRegisterEvent((BindingMemberValue)oldValue, newValue, context);
                RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
            }
            else
            {
                if (_closure != null)
                {
                    _closure.Unsubscribe();
                }
                lastMember.SetValue(penultimateValue, new[] { newValue });
                if (ValueChanged != null)
                {
                    RaiseValueChanged(context, penultimateValue, lastMember, oldValue, newValue, args);
                }
            }
            return(true);
        }