Beispiel #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;
        }
        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;
                }
            }
        }
Beispiel #3
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);
                    }
                }
            }
        }
Beispiel #4
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);
            }
        }
Beispiel #5
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
                    );
            }
        }
Beispiel #6
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));
        }
Beispiel #7
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;
                }
            }
        }
        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);
                }
            }
        }
Beispiel #9
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);
                    }
                }
            }
        }