Beispiel #1
0
        public BindingExpression(DependencyObject target, DependencyProperty targetProperty, PropertyPath path,
                                 object source             = null, RelativeSource relativeSource = null, string elementName = null,
                                 BindingMode mode          = BindingMode.Default, UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.Default,
                                 IValueConverter converter = null, object converterParameter = null, object fallbackValue = null, object targetNullValue = null)
        {
            this.Target              = target;
            this.TargetProperty      = targetProperty;
            this.Path                = path;
            this.Source              = source;
            this.RelativeSource      = relativeSource;
            this.ElementName         = elementName;
            this.Mode                = mode;
            this.UpdateSourceTrigger = updateSourceTrigger;
            this.Converter           = converter;
            this.ConverterParameter  = converterParameter;
            this.FallbackValue       = fallbackValue;
            this.TargetNullValue     = targetNullValue;

            Status = BindingStatus.Inactive;

            disableSourceUpdate = new ReentrancyLock();
            disableTargetUpdate = new ReentrancyLock();

            targetValue = new ObservableValue(Target.GetValue(TargetProperty));
            targetValue.ValueChanged += OnTargetValueChanged;

            BindingMode resolvedBindingMode = Mode == BindingMode.Default ? GetDefaultBindingMode(Target, TargetProperty) : Mode;

            isSourceUpdateMode = resolvedBindingMode == BindingMode.TwoWay || resolvedBindingMode == BindingMode.OneWayToSource;
            isTargetUpdateMode = resolvedBindingMode == BindingMode.TwoWay || resolvedBindingMode == BindingMode.OneWay;

            sourceObserver   = CreateSourceObserver(Target, Source, RelativeSource, ElementName);
            sourceExpression = new ObservableExpression(sourceObserver, Path ?? PropertyPath.Empty);

            // try to update the target (or the source on OneWayToSource)
            if (isTargetUpdateMode)
            {
                sourceExpression.ValueChanged += (sender, oldValue, newValue) => UpdateTargetOnSourceChanged();
                UpdateTargetOnSourceChanged();
            }
            else if (isSourceUpdateMode)
            {
                sourceExpression.ValueChanged += (sender, oldValue, newValue) =>
                {
                    if (Status == BindingStatus.UpdateSourceError && sourceExpression.Value != ObservableValue.UnsetValue && !disableTargetUpdate)
                    {
                        // source was connected
                        UpdateSourceOnTargetChanged();
                    }
                };

                UpdateSourceOnTargetChanged();
            }

            if (UpdateSourceTrigger == UpdateSourceTrigger.LostFocus && isSourceUpdateMode && Target is UIElement)
            {
                ((UIElement)Target).LostFocus += OnLostFocus;
            }
        }
Beispiel #2
0
        public BindingExpression(DependencyObject target, DependencyProperty targetProperty, PropertyPath path,
            object source = null, RelativeSource relativeSource = null, string elementName = null,
            BindingMode mode = BindingMode.Default, UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.Default,
            IValueConverter converter = null, object converterParameter = null, object fallbackValue = null, object targetNullValue = null)
        {
            this.Target = target;
            this.TargetProperty = targetProperty;
            this.Path = path;
            this.Source = source;
            this.RelativeSource = relativeSource;
            this.ElementName = elementName;
            this.Mode = mode;
            this.UpdateSourceTrigger = updateSourceTrigger;
            this.Converter = converter;
            this.ConverterParameter = converterParameter;
            this.FallbackValue = fallbackValue;
            this.TargetNullValue = targetNullValue;

            Status = BindingStatus.Inactive;

            disableSourceUpdate = new ReentrancyLock();
            disableTargetUpdate = new ReentrancyLock();

            targetValue = new ObservableValue(Target.GetValue(TargetProperty));
            targetValue.ValueChanged += OnTargetValueChanged;

            BindingMode resolvedBindingMode = Mode == BindingMode.Default ? GetDefaultBindingMode(Target, TargetProperty) : Mode;

            isSourceUpdateMode = resolvedBindingMode == BindingMode.TwoWay || resolvedBindingMode == BindingMode.OneWayToSource;
            isTargetUpdateMode = resolvedBindingMode == BindingMode.TwoWay || resolvedBindingMode == BindingMode.OneWay;

            object resolvedSource = Source ?? GetRelativeSource(Target, RelativeSource, ElementName);
            sourceExpression = new ObservableExpression(resolvedSource, Path);

            // try to update the target (or the source on OneWayToSource)
            if (isTargetUpdateMode)
            {
                sourceExpression.ValueChanged += (sender, e) => UpdateTargetOnSourceChanged();
                UpdateTargetOnSourceChanged();
            }
            else if (isSourceUpdateMode)
            {
                sourceExpression.ValueChanged += (sender, e) =>
                {
                    if (Status == BindingStatus.UpdateSourceError && sourceExpression.Value != ObservableValue.UnsetValue && !disableTargetUpdate)
                    {
                        // source was connected
                        UpdateSourceOnTargetChanged();
                    }
                };

                UpdateSourceOnTargetChanged();
            }

            if (((RelativeSource != null && RelativeSource.Mode != RelativeSourceMode.Self) || !ElementName.IsNullOrEmpty()) && Target is Visual)
            {
                ((Visual)Target).VisualAncestorChanged += OnTargetVisualAncestorChanged;
            }

            if (UpdateSourceTrigger == UpdateSourceTrigger.LostFocus && isSourceUpdateMode && Target is UIElement)
            {
                ((UIElement)Target).LostFocus += OnLostFocus;
            }
        }