Ejemplo n.º 1
0
        internal override void OnAttach(DependencyObject d, DependencyProperty dp)
        {
            if (IsAttached)
            {
                return;
            }

            this._isAttaching = IsAttached = true;

            this.Target = d;

            this.FindSource();

            // FindSource should find the source now. Otherwise, the PropertyPathNodes
            // shoud do the work (their properties will change when the source will
            // become available)
            propertyPathWalker.Update(this._bindingSource);

            //Listen to changes on the Target if the Binding is TwoWay:
            if (ParentBinding.Mode == BindingMode.TwoWay)
            {
                _propertyListener = INTERNAL_PropertyStore.ListenToChanged(Target, TargetProperty, UpdateSourceCallback);

                // If the user wants to force the Validation of the value when the element
                // is added to the Visual tree, we set a boolean to do it as soon as possible:
                if (ParentBinding.ValidatesOnExceptions && ParentBinding.ValidatesOnLoad)
                {
                    INTERNAL_ForceValidateOnNextSetValue = true;
                }
            }

            this._isAttaching = false;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="WeakPropertyChangedListener"/> class.
 /// </summary>
 /// <param name="source">The source of the PropertyChanged event.</param>
 /// <param name="listener">The listener.</param>
 private WeakPropertyChangedListener(INotifyPropertyChanged source, IPropertyChangedListener listener)
 {
     _syncLock = new object();
     _source   = source;
     _source.PropertyChanged += SourcePropertyChanged;
     _weakListener            = new WeakReference(listener);
 }
Ejemplo n.º 3
0
        internal bool INTERNAL_ForceValidateOnNextSetValue = false; //This boolean is set to true in OnAttached to force Validation at the next UpdateSourceObject. Its purpose is to force the Validation only once to avoid hindering performances.

        internal void OnAttached(DependencyObject target)
        {
            if (IsAttached)
            {
                return;
            }

            IsAttached = true;
            //get the DataContextProperty.
            string             dataContextPropertyName = "DataContext";
            Type               type = target.GetType();
            DependencyProperty dataContextDependencyProperty = INTERNAL_TypeToStringsToDependencyProperties.GetPropertyInTypeOrItsBaseTypes(type, dataContextPropertyName);

            if (dataContextDependencyProperty != null)
            {
                object dataContext = target.GetValue(dataContextDependencyProperty);
                this.OnDataContextChanged(dataContext);
            }

            var source = FindSource();

            PropertyPathWalker.Update(source); //FindSource should find the source now. Otherwise, the PropertyPathNodes shoud do the work (their properties will change when the source will become available)

            //Listen to changes on the Target if the Binding is TwoWay:
            if (ParentBinding.Mode == BindingMode.TwoWay)
            {
                PropertyListener = INTERNAL_PropertyStore.ListenToChanged(Target, Property, UpdateSourceCallback);

                //If the user wants to force the Validation of the value when the element is added to the Visual tree, we set a boolean to do it as soon as possible:
                if (ParentBinding.ValidatesOnExceptions && ParentBinding.ValidatesOnLoad)
                {
                    INTERNAL_ForceValidateOnNextSetValue = true;
                }
            }
        }
Ejemplo n.º 4
0
        internal override void OnSourceChanged(object oldvalue, object newValue)
        {
            DependencyObject oldSource = oldvalue as DependencyObject;
            DependencyObject newSource = newValue as DependencyObject;

            var listener = _dependencyPropertyListener;

            if (listener != null)
            {
                listener.Detach();
                _dependencyPropertyListener = listener = null;
            }

            DependencyProperty = null;
            PropertyInfo       = null;
            FieldInfo          = null;
            if (Source == null)
            {
                return;
            }

            if (newSource != null)
            {
                if (!BindsDirectlyToSource)
                {
                    DependencyProperty dependencyProperty;
                    if (_propertyName == "DataContext")
                    {
                        dependencyProperty = FrameworkElement.DataContextProperty;
                    }
                    else
                    {
                        Type type = Source.GetType();
                        dependencyProperty = INTERNAL_TypeToStringsToDependencyProperties.GetPropertyInTypeOrItsBaseTypes(type, _propertyName);
                    }

                    if (dependencyProperty != null)
                    {
                        this.DependencyProperty          = dependencyProperty;
                        this._dependencyPropertyListener = listener = INTERNAL_PropertyStore.ListenToChanged(newSource, dependencyProperty, this.OnPropertyChanged);
                    }
                } //else (if there is no path), we don't need a listener because changing the source will directly call this method.
            }

            //todo: support attached DependencyProperties
            if (DependencyProperty == null)// || !this.DependencyProperty.IsAttached)
            {
                if (!BindsDirectlyToSource)
                {
                    Type sourceType = Source.GetType();
                    this.PropertyInfo = sourceType.GetProperty(_propertyName);
                    if (this.PropertyInfo == null)
                    {
                        // Try in case it is a simple field instead of a property:
                        this.FieldInfo = sourceType.GetField(_propertyName);
                    }
                }
            }
        }
        internal static WeakPropertyChangedListener CreateIfNecessary(object source, IPropertyChangedListener listener)
        {
            var inpc = source as INotifyPropertyChanged;

            if (inpc != null)
            {
                return(new WeakPropertyChangedListener(inpc, listener));
            }
            return(null);
        }
Ejemplo n.º 6
0
        internal override void OnDetach(DependencyObject d, DependencyProperty dp)
        {
            if (!IsAttached)
            {
                return;
            }

            IsAttached = false;

            _skipTypeCheck = false;
            var listener = _listener;

            _listener = null;
            listener?.Detach();
        }
 private void SourcePropertyChanged(object sender, PropertyChangedEventArgs e)
 {
     if (this.weakListener != null)
     {
         IPropertyChangedListener target = this.weakListener.Target as IPropertyChangedListener;
         if (target != null)
         {
             target.OnPropertyChanged(sender, e);
         }
         else
         {
             this.Detach();
         }
     }
 }
Ejemplo n.º 8
0
        internal override void OnAttached(DependencyObject target)
        {
            if (IsAttached)
            {
                return;
            }
            base.OnAttached(target);
            var source = FindSource();

            PropertyPathWalker.Update(source); //FindSource should find the source now. Otherwise, the PropertyPathNodes shoud do the work (their properties will change when the source will become available)

            if (ParentBinding.Mode == BindingMode.TwoWay)
            {
                PropertyListener = INTERNAL_PropertyStore.ListenToChanged(Target, Property, UpdateSourceCallback);
            }
        }
Ejemplo n.º 9
0
        internal override void OnDetached(DependencyObject element)
        {
            if (IsAttached)
            {
                base.OnDetached(element);
                if (PropertyListener != null)
                {
                    PropertyListener.Detach();
                    PropertyListener = null;
                }

                PropertyPathWalker.Update(null);

                Target = null;
            }
        }
Ejemplo n.º 10
0
        internal void OnDetached(DependencyObject element)
        {
            if (IsAttached)
            {
                this.IsAttached = false;
                this.OnDataContextChanged(null);

                if (PropertyListener != null)
                {
                    PropertyListener.Detach();
                    PropertyListener = null;
                }

                PropertyPathWalker.Update(null);

                Target = null;
            }
        }
Ejemplo n.º 11
0
        internal void OnDetached(DependencyObject element)
        {
            if (IsAttached)
            {
                this.IsAttached = false;

                if (_propertyListener != null)
                {
                    _propertyListener.Detach();
                    _propertyListener = null;
                }

                propertyPathWalker.Update(null);

                Target.InheritedContextChanged -= new EventHandler(this.OnTargetInheritedContextChanged);
                Target = null;
            }
        }
Ejemplo n.º 12
0
        internal override void OnSourceChanged(object oldvalue, object newValue)
        {
            IPropertyChangedListener listener = _dpListener;

            if (listener != null)
            {
                _dpListener = null;
                listener.Detach();
            }

            DependencyObject sourceDO = SourceDO;

            if (sourceDO != null)
            {
                _dpListener = INTERNAL_PropertyStore.ListenToChanged(
                    sourceDO, _dp, OnPropertyChanged
                    );
            }
        }
Ejemplo n.º 13
0
        internal override void OnAttach(DependencyObject d, DependencyProperty dp)
        {
            if (IsAttached)
            {
                return;
            }

            Debug.Assert(d != null);
            Debug.Assert(dp != null);

            IsAttached = true;

            Target         = d;
            TargetProperty = dp;

            _skipTypeCheck = TargetProperty.PropertyType.IsAssignableFrom(SourceProperty.PropertyType);
            _listener      = INTERNAL_PropertyStore.ListenToChanged(Source, SourceProperty,
                                                                    (o, args) => Target.ApplyExpression(TargetProperty, this, false));
        }
Ejemplo n.º 14
0
        internal override void OnDetach(DependencyObject d, DependencyProperty dp)
        {
            if (!IsAttached)
            {
                return;
            }

            this.IsAttached = false;

            if (_propertyListener != null)
            {
                _propertyListener.Detach();
                _propertyListener = null;
            }

            propertyPathWalker.Update(null);

            Target.InheritedContextChanged -= new EventHandler(this.OnTargetInheritedContextChanged);
            Target = null;
        }
Ejemplo n.º 15
0
        internal bool INTERNAL_ForceValidateOnNextSetValue = false; //This boolean is set to true in OnAttached to force Validation at the next UpdateSourceObject. Its purpose is to force the Validation only once to avoid hindering performances.

        internal override void OnAttached(DependencyObject target)
        {
            if (IsAttached)
            {
                return;
            }
            base.OnAttached(target);
            var source = FindSource();

            PropertyPathWalker.Update(source); //FindSource should find the source now. Otherwise, the PropertyPathNodes shoud do the work (their properties will change when the source will become available)

            //Listen to changes on the Target if the Binding is TwoWay:
            if (ParentBinding.Mode == BindingMode.TwoWay)
            {
                PropertyListener = INTERNAL_PropertyStore.ListenToChanged(Target, Property, UpdateSourceCallback);

                //If the user wants to force the Validation of the value when the element is added to the Visual tree, we set a boolean to do it as soon as possible:
                if (ParentBinding.ValidatesOnExceptions && ParentBinding.ValidatesOnLoad)
                {
                    INTERNAL_ForceValidateOnNextSetValue = true;
                }
            }
        }
 private WeakPropertyChangedListener(INotifyPropertyChanged source, IPropertyChangedListener listener)
 {
     this.source = source;
     this.source.PropertyChanged += new PropertyChangedEventHandler(this.SourcePropertyChanged);
     this.weakListener            = new WeakReference(listener);
 }
 public void AddPropertyChangedListener(IPropertyChangedListener propertyChangedListener)
 {
     notifier.AddPropertyChangedListener(propertyChangedListener);
 }
 public void RemovePropertyChangedListener(IPropertyChangedListener propertyChangedListener)
 {
     notifier.RemovePropertyChangedListener(propertyChangedListener);
 }
        /// <summary>
        /// Creates a new WeakPropertyChangedListener if the source implements INotifyPropertyChanged
        /// and registers for the PropertyChanged event.
        /// </summary>
        /// <param name="source">The source INotifyPropertyChanged.</param>
        /// <param name="listener">The listener object.</param>
        /// <returns></returns>
        public static WeakPropertyChangedListener CreateIfNecessary(object source, IPropertyChangedListener listener)
        {
            INotifyPropertyChanged inpc = source as INotifyPropertyChanged;

            return(inpc != null ? new WeakPropertyChangedListener(inpc, listener) : null);
        }
Ejemplo n.º 20
0
        internal override void OnSourceChanged(object oldValue, object newValue)
        {
            if (oldValue is INotifyPropertyChanged inpc)
            {
                inpc.PropertyChanged -= new PropertyChangedEventHandler(OnSourcePropertyChanged);
            }

            IPropertyChangedListener listener = _dpListener;

            if (listener != null)
            {
                _dpListener = null;
                listener.Detach();
            }

            _dp    = null;
            _prop  = null;
            _field = null;

            if (Source == null)
            {
                return;
            }

            if (_bindsDirectlyToSource)
            {
                return;
            }

            inpc = newValue as INotifyPropertyChanged;
            if (inpc != null)
            {
                inpc.PropertyChanged += new PropertyChangedEventHandler(OnSourcePropertyChanged);
            }

            if (newValue is DependencyObject sourceDO)
            {
                Type type = _resolvedType ?? Source.GetType();

                DependencyProperty dependencyProperty = INTERNAL_TypeToStringsToDependencyProperties.GetPropertyInTypeOrItsBaseTypes(type, _propertyName);

                if (dependencyProperty != null)
                {
                    _dp         = dependencyProperty;
                    _dpListener = listener = INTERNAL_PropertyStore.ListenToChanged(sourceDO, dependencyProperty, OnPropertyChanged);
                }
            }

            if (_dp == null)
            {
                Type sourceType = Source.GetType();
                for (Type t = sourceType; t != null; t = t.BaseType)
                {
                    _prop = t.GetProperty(
                        _propertyName,
                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly
                        );

                    if (_prop != null)
                    {
                        break;
                    }
                }

                if (_prop == null)
                {
                    // Try in case it is a simple field instead of a property:
                    _field = sourceType.GetField(_propertyName);
                }
            }
        }
Ejemplo n.º 21
0
 public void AddPropertyChangedListener(IPropertyChangedListener propertyChangeListener)
 {
     this.notifier.AddPropertyChangedListener(propertyChangeListener);
 }
Ejemplo n.º 22
0
        internal override void OnSourceChanged(object oldvalue, object newValue)
        {
            DependencyObject oldSource = null;
            DependencyObject newSource = null;

            if (oldvalue is DependencyObject)
            {
                oldSource = (DependencyObject)oldvalue;
            }
            if (newValue is DependencyObject)
            {
                newSource = (DependencyObject)newValue;
            }

            var listener = _dependencyPropertyListener;

            if (listener != null)
            {
                listener.Detach();
                _dependencyPropertyListener = listener = null;
            }

            DependencyProperty = null;
            PropertyInfo       = null;
            FieldInfo          = null;
            if (Source == null)
            {
                return;
            }

            if (newSource != null)
            {
                if (!BindsDirectlyToSource)
                {
                    DependencyProperty dependencyProperty;
                    if (_propertyName == "DataContext") // Note: we handle the special case of the DataContext because the "DataContext" property is defined in the "UIElement" class, and some classes such as "RotateTransform" inherit the DataContext property even though they are not UIElements, so if we looked for the "DataContext" property in the type (and its base types) we wouldn't find it.
                    {
                        dependencyProperty = FrameworkElement.DataContextProperty;
                    }
                    else
                    {
                        Type type = Source.GetType();
                        dependencyProperty = INTERNAL_TypeToStringsToDependencyProperties.GetPropertyInTypeOrItsBaseTypes(type, _propertyName);
                    }

                    if (dependencyProperty != null)
                    {
                        this.DependencyProperty          = dependencyProperty;
                        this._dependencyPropertyListener = listener = INTERNAL_PropertyStore.ListenToChanged(newSource, dependencyProperty, this.OnPropertyChanged);
                    }
                } //else (if there is no path), we don't need a listener because changing the source will directly call this method.
            }

            //todo: support attached DependencyProperties
            if (DependencyProperty == null)// || !this.DependencyProperty.IsAttached)
            {
                if (!BindsDirectlyToSource)
                {
                    Type sourceType = Source.GetType();
                    this.PropertyInfo = sourceType.GetProperty(_propertyName);
                    if (this.PropertyInfo == null)
                    {
                        // Try in case it is a simple field instead of a property:
                        this.FieldInfo = sourceType.GetField(_propertyName);
                    }
                }
            }
        }
Ejemplo n.º 23
0
 public void RemovePropertyChangedListener(IPropertyChangedListener propertyChangeListener)
 {
     this.notifier.RemovePropertyChangedListener(propertyChangeListener);
 }