Example #1
0
        /// <summary>
        /// Creates a binding.
        /// </summary>
        /// <param name="trigger">The trigger.</param>
        /// <param name="applyConverter">Assign the converter if set to <c>true</c>.</param>
        /// <returns>
        /// The binding.
        /// </returns>
        public virtual Binding CreateBinding(UpdateSourceTrigger trigger = UpdateSourceTrigger.Default, bool applyConverter = true)
        {
            var bindingMode  = this.Descriptor.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
            var formatString = this.FormatString;

            if (formatString != null && !formatString.StartsWith("{"))
            {
                formatString = "{0:" + formatString + "}";
            }

            var binding = new Binding(this.PropertyName)
            {
                Mode                  = bindingMode,
                Converter             = applyConverter ? this.Converter : null,
                ConverterParameter    = this.ConverterParameter,
                StringFormat          = formatString,
                UpdateSourceTrigger   = trigger,
                ValidatesOnDataErrors = true,
                ValidatesOnExceptions = true
            };

            if (this.ConverterCulture != null)
            {
                binding.ConverterCulture = this.ConverterCulture;
            }

            return(binding);
        }
Example #2
0
        public static void BindingPropertyIfUndefaultAndUninherited(FrameworkElement element,
                                                                    DependencyProperty targetProperty,
                                                                    object source,
                                                                    DependencyProperty sourceProperty,
                                                                    IValueConverter converter   = null,
                                                                    BindingMode mode            = BindingMode.Default,
                                                                    UpdateSourceTrigger trigger = UpdateSourceTrigger.Default)
        {
            var baseValueSource = DependencyPropertyHelper.GetValueSource(element, targetProperty).BaseValueSource;

            if (baseValueSource != BaseValueSource.Default && baseValueSource != BaseValueSource.Inherited)
            {
                return;
            }
            var binding = new Binding()
            {
                Path                = new PropertyPath(sourceProperty),
                Source              = source,
                Mode                = mode,
                Converter           = converter,
                UpdateSourceTrigger = trigger
            };

            element.SetBinding(targetProperty, binding);
        }
Example #3
0
        public DesignItemBinding(string path)
        {
            this._propertyName = path;

            UpdateSourceTrigger          = UpdateSourceTrigger.Default;
            AskWhenMultipleItemsSelected = true;
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Binding"/> class with initial
        /// property values copied from the specified <see cref="Binding"/>.
        /// </summary>
        /// <param name="original">
        /// The <see cref="Binding"/> to copy.
        /// </param>
        public Binding(Binding original)
        {
            if (original != null)
            {
                FallbackValue   = original.FallbackValue;
                TargetNullValue = original.TargetNullValue;
                StringFormat    = original.StringFormat;

                _isInStyle = original._isInStyle;
                _converter = original._converter;
#if MIGRATION
                _culture = original._culture;
#else
                _culture = original._culture;
#endif
                _converterParameter = original._converterParameter;
                _elementName        = original._elementName;
                _mode                        = original._mode;
                _xamlPath                    = original._xamlPath;
                _path                        = original._path;
                _relativeSource              = original._relativeSource;
                _source                      = original._source;
                _updateSourceTrigger         = original._updateSourceTrigger;
                _validatesOnExceptions       = original._validatesOnExceptions;
                _notifyOnValidationError     = original._notifyOnValidationError;
                _bindsDirectlyToSource       = original._bindsDirectlyToSource;
                _validatesOnNotifyDataErrors = original._validatesOnNotifyDataErrors;
                _validatesOnDataErrors       = original._validatesOnDataErrors;

                _wasModeSetByUserRatherThanDefaultValue = original._wasModeSetByUserRatherThanDefaultValue;
            }
        }
Example #5
0
        /// <summary>
        /// method used to force the datbabinding for dynamic forms
        ///
        /// </summary>
        /// <param name="view">view for witch binding is forced</param>
        public static void ForceDataBind(FrameworkElement view)
        {
            FindBindingsRecursively(view,
                                    delegate(FrameworkElement element, Binding binding, DependencyProperty dp)
            {
                if (element.DataContext != null)
                {
                    UpdateSourceTrigger updateTrigger = binding.UpdateSourceTrigger;
                    PropertyPath path         = binding.Path;
                    BindingMode mode          = binding.Mode;
                    IValueConverter converter = binding.Converter;
                    ArrayList rules           = new ArrayList();
                    foreach (ValidationRule rule in binding.ValidationRules)
                    {
                        rules.Add(rule);
                    }
                    BindingOperations.ClearBinding(element, dp);

                    Binding newBinding             = new Binding();
                    newBinding.Source              = element.DataContext;
                    newBinding.Mode                = mode;
                    newBinding.Converter           = converter;
                    newBinding.Path                = path;
                    newBinding.UpdateSourceTrigger = updateTrigger;
                    foreach (ValidationRule rule in rules)
                    {
                        newBinding.ValidationRules.Add(rule);
                    }
                    BindingOperations.SetBinding(element, dp, newBinding);
                }
            });
        }
 public DataBindingAttribute(Type sourceType, string source, BindingMode mode, UpdateSourceTrigger updateSourceTrigger)
 {
     SourceType          = sourceType;
     Source              = source;
     Mode                = mode;
     UpdateSourceTrigger = updateSourceTrigger;
 }
Example #7
0
        public static Binding SetBinding(
            DependencyObject source, string sourcePropertyPath,
            DependencyObject target, DependencyProperty targetProperty,
            BindingMode mode = BindingMode.TwoWay,
            UpdateSourceTrigger sourceTrigger = UpdateSourceTrigger.Default,
            IValueConverter converter         = null,
            object fallbackValue = null)
        {
            var result = new Binding(sourcePropertyPath)
            {
                Source              = source,
                Mode                = mode,
                Converter           = converter,
                UpdateSourceTrigger = sourceTrigger
            };

            if (fallbackValue != null)
            {
                result.FallbackValue = fallbackValue;
            }

            BindingOperations.SetBinding(target, targetProperty, result);

            return(result);
        }
Example #8
0
        /// <summary>Destinations the specified get target.</summary>
        /// <typeparam name="TTarget">The type of the target.</typeparam>
        /// <typeparam name="TValue">The type of the target value.</typeparam>
        /// <param name="id">The id.</param>
        /// <param name="target">The target.</param>
        /// <param name="sourceProperty">The source property.</param>
        /// <param name="getSourceValue">The get source value.</param>
        /// <param name="targetProperty">The target property.</param>
        /// <param name="getTargetValue">The get target value.</param>
        /// <param name="setSourceValue">The set source value.</param>
        /// <param name="bindingMode">The binding mode.</param>
        /// <param name="updateSourceTrigger">The update source trigger.</param>
        /// <returns>A new destination.</returns>
        public DataBinding <TRoot, TSource, TTarget, TValue> BindInvariant <TTarget, TValue>(
            int id,
            TTarget target,
            INotifyingProperty <TSource> sourceProperty,
            Func <TSource, TValue> getSourceValue,
            DependencyProperty targetProperty,
            Func <TTarget, TValue> getTargetValue,
            Action <TSource, TValue> setSourceValue,
            BindingMode bindingMode,
            UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.Default)
            where TTarget : DependencyObject
        {
            var dataDestination = new DataBinding <TRoot, TSource, TTarget, TValue>(
                id,
                this,
                target,
                sourceProperty,
                getSourceValue,
                targetProperty,
                getTargetValue,
                setSourceValue,
                updateSourceTrigger,
                bindingMode);

            this.bindings.Add(dataDestination);
            return(dataDestination);
        }
Example #9
0
 public static BindingExpressionBase SetBinding(
     DependencyObject target,
     DependencyProperty dp,
     string path,
     BindingMode mode            = BindingMode.OneWay,
     UpdateSourceTrigger trigger = UpdateSourceTrigger.Default,
     IValueConverter converter   = null,
     object converterParameter   = null,
     string stringFormat         = null,
     object fallBackValue        = null,
     int delay    = 0,
     bool isAsync = false)
 {
     return(SetBinding(
                target,
                dp,
                null,
                path,
                mode,
                trigger,
                converter,
                converterParameter,
                stringFormat,
                fallBackValue,
                delay,
                isAsync));
 }
 internal static void OverrideMetadataWithUpdateSourceTrigger(
     this DependencyProperty property,
     Type forType,
     UpdateSourceTrigger updateSourceTrigger)
 {
     OverrideMetadataWithUpdateSourceTrigger(property, property.OwnerType, forType, updateSourceTrigger);
 }
        private static DataTemplate CreateDefaultElementTemplated(DataGrid dataGrid,
                                                                  string propertyName,
                                                                  BindingMode bindingMode,
                                                                  UpdateSourceTrigger updateSourceTrigger,
                                                                  string stringFormat)
        {
            var factory = new FrameworkElementFactory(typeof(TextBlock));

            factory.SetValue(TextBlock.StyleProperty, CreateDefaultElementStyle(dataGrid));
            factory.SetBinding(TextBlock.TextProperty, new Binding(propertyName)
            {
                Mode = bindingMode,
                UpdateSourceTrigger = updateSourceTrigger,
                StringFormat        = stringFormat
            });
            factory.SetBinding(TextBlock.HorizontalAlignmentProperty, new Binding()
            {
                Path   = new PropertyPath(DataGrid.HorizontalContentAlignmentProperty),
                Source = dataGrid,
            });
            return(new DataTemplate()
            {
                VisualTree = factory,
            });
        }
Example #12
0
        /// <summary>
        /// Create a Binder
        /// </summary>
        /// <remarks>
        /// Specifying which binder to use can be acheived by placing a BindingOptionsControl on the page or
        /// using the BindingStateMode config key. BindingOptionsControl takes precedence. A default ViewStateBinder
        /// is used in the absence of the above.
        /// </remarks>
        /// <param name="bindingContainer"></param>
        /// <returns></returns>
        public static BinderBase CreateBinder(IBindingContainer bindingContainer)
        {
            //Create a binder based on:
            //A) The binder specified by a binding options control
            BinderBase binder = null;

            if (!TryGetBinderFromOptionsControl(bindingContainer, out binder))
            {
                //B) The binder specified in the config
                if (!TryGetBinderFromConfig(bindingContainer, out binder))
                {
                    //C) the default binder is created.
                    binder = CreateBinder(bindingContainer, DEFAULT_STATE_MODE);
                }
            }

            //Set the update source trigger on:
            UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.PostBack;

            //A) The updateSourceTrigger specified by a binding options control
            if (!TryGetUpdateSourceTriggerOptionsControl(bindingContainer, out updateSourceTrigger))
            {
                //B) The updateSourceTrigger specified in the config
                TryGetUpdateSourceTriggerFromConfig(bindingContainer, out updateSourceTrigger);
            }

            binder.UpdateSourceTrigger = updateSourceTrigger;

            return(binder);
        }
        /// <summary>
        /// Creates a binding.
        /// </summary>
        /// <param name="bindingPath">The binding path.</param>
        /// <param name="trigger">The trigger.</param>
        /// <returns>
        /// A binding.
        /// </returns>
        public Binding CreateBinding(string bindingPath, UpdateSourceTrigger trigger = UpdateSourceTrigger.Default)
        {
            var bindingMode  = this.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
            var formatString = this.FormatString;

            if (formatString != null && !formatString.StartsWith("{"))
            {
                formatString = "{0:" + formatString + "}";
            }

            var binding = new Binding(bindingPath)
            {
                Mode                  = bindingMode,
                Converter             = this.Converter,
                ConverterParameter    = this.ConverterParameter,
                StringFormat          = formatString,
                UpdateSourceTrigger   = trigger,
                ValidatesOnDataErrors = true,
                ValidatesOnExceptions = true,
                NotifyOnSourceUpdated = true
            };

            if (this.ConverterCulture != null)
            {
                binding.ConverterCulture = this.ConverterCulture;
            }

            return(binding);
        }
Example #14
0
		public DesignItemBinding(string path)
		{
			this._propertyName = path;
			
			UpdateSourceTrigger = UpdateSourceTrigger.Default;
			AskWhenMultipleItemsSelected = true;
		}
Example #15
0
        public static Binding TryBinding(
            DependencyObject source, string sourcePropertyPath,
            DependencyObject target, DependencyProperty targetProperty,
            BindingMode mode = BindingMode.TwoWay,
            UpdateSourceTrigger sourceTrigger = UpdateSourceTrigger.Default,
            IValueConverter converter         = null,
            object fallbackValue = null)
        {
            var sourceProperty = source.FindDependencyProperty(sourcePropertyPath);

            if (sourceProperty != null)
            {
                if (source.GetValue(sourceProperty) == null &&
                    sourceProperty.PropertyType == targetProperty.PropertyType)
                {
                    source.SetValue(sourceProperty, target.GetValue(targetProperty));
                }

                return(BindingHelper.SetBinding(
                           source, sourceProperty,
                           target, targetProperty,
                           mode, sourceTrigger, converter, fallbackValue));
            }

            return(null);
        }
Example #16
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;
            }
        }
 public static void TryRegisterLostFocus <TTarget>(TTarget target, UpdateSourceTrigger updateSourceTrigger, RoutedEventHandler lostFocusEventHandler)
     where TTarget : DependencyObject
 {
     if (updateSourceTrigger == UpdateSourceTrigger.LostFocus && target is UIElement newUiElement)
     {
         newUiElement.LostFocus += lostFocusEventHandler;
     }
 }
        public void SetBinding(string path, BindingMode vMode, UpdateSourceTrigger vTrigger)
        {
            Binding b = new Binding(path);

            b.UpdateSourceTrigger = vTrigger;
            b.Mode = vMode;
            tBox.SetBinding(TextBox.TextProperty, b);
        }
Example #19
0
        public static BindingExpressionBase SetBinding(
            DependencyObject target,
            DependencyProperty dp,
            object source,
            string path,
            BindingMode mode            = BindingMode.OneWay,
            UpdateSourceTrigger trigger = UpdateSourceTrigger.Default,
            IValueConverter converter   = null,
            object converterParameter   = null,
            string stringFormat         = null,
            object fallBackValue        = null,
            int delay    = 0,
            bool isAsync = false)
        {
            var binding = new Binding
            {
                Path = new PropertyPath(path),
                Mode = mode,
                UpdateSourceTrigger = trigger
            };

            if (converter != null)
            {
                binding.Converter = converter;
            }

            if (converterParameter != null)
            {
                binding.ConverterParameter = converterParameter;
            }

            if (source != null)
            {
                binding.Source = source;
            }

            if (fallBackValue != null)
            {
                binding.FallbackValue = fallBackValue;
            }

            if (delay > 0)
            {
                binding.Delay = delay;
            }

            if (!stringFormat.IsNullOrEmpty())
            {
                binding.StringFormat = stringFormat;
            }

            if (isAsync)
            {
                binding.IsAsync = true;
            }

            return(BindingOperations.SetBinding(target, dp, binding));
        }
 public FrameworkPropertyMetadata(object defaultValue = null, PropertyChangedCallback propertyChangedCallback = null, CoerceValueCallback coerceValueCallback = null, bool inherits = false,
     bool affectsMeasure = false, bool affectsArrange = false, bool bindsTwoWayByDefault = false, UpdateSourceTrigger defaultUpdateSourceTrigger = UpdateSourceTrigger.Default)
     : base(defaultValue, propertyChangedCallback, coerceValueCallback, inherits)
 {
     this.AffectsMeasure = affectsMeasure;
     this.AffectsArrange = affectsArrange;
     this.BindsTwoWayByDefault = bindsTwoWayByDefault;
     this.DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
 }
 public FrameworkPropertyMetadata(object defaultValue = null, PropertyChangedCallback propertyChangedCallback = null, CoerceValueCallback coerceValueCallback = null, bool inherits = false,
                                  bool affectsMeasure = false, bool affectsArrange = false, bool bindsTwoWayByDefault = false, UpdateSourceTrigger defaultUpdateSourceTrigger       = UpdateSourceTrigger.Default) :
     base(defaultValue, propertyChangedCallback, coerceValueCallback, inherits)
 {
     this.AffectsMeasure             = affectsMeasure;
     this.AffectsArrange             = affectsArrange;
     this.BindsTwoWayByDefault       = bindsTwoWayByDefault;
     this.DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
 }
Example #22
0
 public DataBindingState(
     IBindingContext <TRoot, TSource> bindingContext,
     TTarget target,
     DependencyProperty targetProperty,
     Func <TTarget, TTargetValue> getTargetValue,
     Func <TSource, TSourceValue> getSourceValue,
     BindingMode bindingMode,
     UpdateSourceTrigger updateSourceTrigger,
     in ConversionParameters <TSourceValue, TTargetValue> conversionParameters)
Example #23
0
 public PropertyDescriptorAttribute()
 {
     Class = string.Empty;
     UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
     Gallery             = string.Empty;
     Catalog             = string.Empty;
     Editable            = true;
     Column     = 2;
     ColumnSpan = 1;
 }
Example #24
0
 public FrameworkPropertyMetadata(object defaultValue, FrameworkPropertyMetadataOptions flags, PropertyChangedCallback propertyChangedCallback, CoerceValueCallback coerceValueCallback, bool isAnimationProhibited, UpdateSourceTrigger defaultUpdateSourceTrigger) :
     base(defaultValue, propertyChangedCallback, coerceValueCallback)
 {
     this.AffectsArrange             = (flags & FrameworkPropertyMetadataOptions.AffectsArrange) != 0;
     this.AffectsMeasure             = (flags & FrameworkPropertyMetadataOptions.AffectsMeasure) != 0;
     this.AffectsVisualState         = (flags & FrameworkPropertyMetadataOptions.AffectsVisualState) != 0;
     this.BindsTwoWayByDefault       = (flags & FrameworkPropertyMetadataOptions.BindsTwoWayByDefault) != 0;
     this.Inherits                   = (flags & FrameworkPropertyMetadataOptions.Inherits) != 0;
     this.DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
 }
Example #25
0
 public static BindingExpressionBase Bind(
     DependencyObject source,
     DependencyProperty path,
     DependencyObject target,
     DependencyProperty targetProp,
     BindingMode mode,
     UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.PropertyChanged)
 {
     return(Bind(source, path.Name, target, targetProp, mode, updateSourceTrigger));
 }
Example #26
0
 internal FrameworkPropertyMetadata(
     object defaultValue,
     FrameworkPropertyMetadataOptions options,
     PropertyChangedCallback propertyChangedCallback,
     CoerceValueCallback coerceValueCallback,
     UpdateSourceTrigger defaultUpdateSourceTrigger
     ) : base(defaultValue, propertyChangedCallback, coerceValueCallback, null)
 {
     Options = options.WithDefault();
     DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
 }
Example #27
0
 public FrameworkPropertyMetadata(
     object defaultValue,
     FrameworkPropertyMetadataOptions flags,
     PropertyChangedCallback propertyChangedCallback,
     CoerceValueCallback coerceValueCallback,
     bool isAnimationProhibited,
     UpdateSourceTrigger defaultUpdateSourceTrigger)
     : base(defaultValue, propertyChangedCallback, coerceValueCallback, isAnimationProhibited)
 {
     this.DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
     this.LoadFlags(flags);
 }
        private static DataTemplate CreateTemplate(DataGrid dataGrid,
                                                   Type elementType,
                                                   DependencyProperty property,
                                                   object elementStyleKey,
                                                   string propertyName,
                                                   BindingMode bindingMode,
                                                   UpdateSourceTrigger updateSourceTrigger,
                                                   string stringFormat)
        {
            var factory = new FrameworkElementFactory(elementType);

            factory.SetBinding(property, new Binding(propertyName)
            {
                Mode = bindingMode,
                UpdateSourceTrigger = updateSourceTrigger,
                StringFormat        = stringFormat,
            });
            if (typeof(FrameworkElement).IsAssignableFrom(elementType))
            {
                factory.SetBinding(FrameworkElement.DataContextProperty, new Binding()
                {
                    Path                = new PropertyPath(DataGridRow.DataContextProperty),
                    RelativeSource      = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1),
                    Mode                = bindingMode,
                    UpdateSourceTrigger = updateSourceTrigger,
                });
            }
            if (elementStyleKey != null)
            {
                factory.SetValue(FrameworkElement.StyleProperty, dataGrid.FindResource(elementStyleKey) as Style);
            }
            if (typeof(Control).IsAssignableFrom(elementType))
            {
                factory.SetBinding(Control.HorizontalContentAlignmentProperty, new Binding()
                {
                    Path   = new PropertyPath(DataGrid.HorizontalContentAlignmentProperty),
                    Source = dataGrid,
                });
            }
            else
            {
                factory.SetBinding(FrameworkElement.HorizontalAlignmentProperty, new Binding()
                {
                    Path   = new PropertyPath(DataGrid.HorizontalContentAlignmentProperty),
                    Source = dataGrid,
                });
            }

            return(new DataTemplate()
            {
                VisualTree = factory,
            });
        }
Example #29
0
        public static BindingExpressionBase Bind(
            DependencyObject source,
            DependencyProperty path1,
            DependencyProperty path2,
            DependencyObject target,
            DependencyProperty targetProp,
            BindingMode mode,
            UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.PropertyChanged)
        {
            var path = $"{path1.Name}.{path2.Name}";

            return(Bind(source, new PropertyPath(path), target, targetProp, mode, updateSourceTrigger));
        }
Example #30
0
 internal BindingBase(object source, PropertyPath sourcePath, object target, PropertyPath targetPath, IValueConverter converter = null, object converterParameter = null, UpdateSourceTrigger updateSourceTrigger = UpdateSourceTrigger.PropertyChanged, BindingMode model = BindingMode.OneWay)
 {
     this.source         = source ?? throw new ArgumentNullException(nameof(source));
     SourcePath          = sourcePath;
     Target              = target;
     TargetPath          = targetPath ?? throw new ArgumentNullException(nameof(targetPath));
     Converter           = converter;
     ConverterParameter  = converterParameter;
     UpdateSourceTrigger = updateSourceTrigger;
     Model = model;
     sourcePath?.SetRelySource(source);
     targetPath?.SetRelySource(target);
     Validates = new Collection <IValidateValue>();
 }
Example #31
0
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------

        // return false if this is an invalid value for UpdateSourceTrigger
        internal static bool IsValidUpdateSourceTrigger(UpdateSourceTrigger value)
        {
            switch (value)
            {
            case UpdateSourceTrigger.Default:
            case UpdateSourceTrigger.PropertyChanged:
            case UpdateSourceTrigger.LostFocus:
            case UpdateSourceTrigger.Explicit:
                return(true);

            default:
                return(false);
            }
        }
        private void BindField(string fieldName,
                               DependencyObject control,
                               DependencyProperty controlProperty,
                               BindingMode mode            = BindingMode.Default,
                               UpdateSourceTrigger trigger = UpdateSourceTrigger.Default)
        {
            Binding bind = new Binding();

            bind.Source = this.Model;
            bind.Path   = new PropertyPath(fieldName);
            bind.Mode   = mode;
            bind.UpdateSourceTrigger = trigger;
            BindingOperations.SetBinding(control, controlProperty, bind);
        }
Example #33
0
 /// <summary>
 /// Creates a new <see cref="BindingDependency"/> object.
 /// </summary>
 /// <param name="sourceDd">Souce data descriptor for the dependency.</param>
 /// <param name="targetDd">Target data descriptor for the dependency.</param>
 /// <param name="autoAttachToSource">If set to <c>true</c>, the new dependency object will be
 /// automatically attached to the <paramref name="sourceDd"/> data descriptor. This means it will
 /// capture changes from it and reflect them on the <paramref name="targetDd"/> data descriptor.</param>
 /// <param name="updateSourceTrigger">This parameter controls, which target object event makes this
 /// binding dependency copy the target value to the <paramref name="sourceDd"/> data descriptor.
 /// If set to <see cref="UpdateSourceTrigger.PropertyChanged"/>, the new binding dependency object
 /// will automatically attach to property changes of the <paramref name="targetDd"/> data descriptor and
 /// reflect the changed value to the <paramref name="sourceDd"/> data descriptor. If set to
 /// <see cref="UpdateSourceTrigger.LostFocus"/>, the new binding dependency will attach to the
 /// <see cref="UIElement.EventOccured"/> event of the <paramref name="parentUiElement"/> object.
 /// If set to <see cref="UpdateSourceTrigger.Explicit"/>, the new binding dependency won't attach to
 /// the target at all.</param>
 /// <param name="parentUiElement">The parent <see cref="UIElement"/> of the specified <paramref name="targetDd"/>
 /// data descriptor. This parameter is only used to attach to the lost focus event if
 /// <paramref name="updateSourceTrigger"/> is set to <see cref="UpdateSourceTrigger.LostFocus"/>.</param>
 /// <param name="customValueConverter">Set a custom value converter with this parameter. If this parameter
 /// is set to <c>null</c>, the default <see cref="TypeConverter"/> will be used.</param>
 /// <param name="customValueConverterParameter">Parameter to be used in the custom value converter, if one is
 /// set.</param>
 public BindingDependency(IDataDescriptor sourceDd, IDataDescriptor targetDd, bool autoAttachToSource,
     UpdateSourceTrigger updateSourceTrigger, UIElement parentUiElement,
     IValueConverter customValueConverter, object customValueConverterParameter)
 {
   _sourceDd = sourceDd;
   _targetDd = targetDd;
   _targetObject = _targetDd.TargetObject as DependencyObject;
   _sourceObject = _sourceDd.TargetObject as DependencyObject;
   _valueConverter = customValueConverter;
   _converterParameter = customValueConverterParameter;
   if (autoAttachToSource && sourceDd.SupportsChangeNotification)
   {
     sourceDd.Attach(OnSourceChanged);
     _attachedToSource = true;
   }
   if (targetDd.SupportsChangeNotification)
   {
     if (updateSourceTrigger == UpdateSourceTrigger.PropertyChanged)
     {
       targetDd.Attach(OnTargetChanged);
       _attachedToTarget = true;
     }
     else if (updateSourceTrigger == UpdateSourceTrigger.LostFocus)
     {
       if (parentUiElement != null)
         parentUiElement.EventOccured += OnTargetElementEventOccured;
       _attachedToLostFocus = parentUiElement;
     }
   }
   // Initially update endpoints
   if (autoAttachToSource)
     UpdateTarget();
   if (updateSourceTrigger != UpdateSourceTrigger.Explicit &&
       !autoAttachToSource) // If we are attached to both, only update one direction
     UpdateSource();
 }
 public ControlPropertyMetadata(object defaultValue = null, PropertyChangedCallback propertyChangedCallback = null, CoerceValueCallback coerceValueCallback = null, bool inherits = false,
     bool affectsMeasure = false, bool affectsArrange = false, bool affectsVisualState = false, bool bindsTwoWayByDefault = true, UpdateSourceTrigger defaultUpdateSourceTrigger = UpdateSourceTrigger.Default)
     : base(defaultValue, propertyChangedCallback, coerceValueCallback, inherits, affectsMeasure, affectsArrange, bindsTwoWayByDefault, defaultUpdateSourceTrigger)
 {
     this.AffectsVisualState = affectsVisualState;
 }
 public FrameworkPropertyMetadata(
             object defaultValue,
             FrameworkPropertyMetadataOptions flags,
             PropertyChangedCallback propertyChangedCallback,
             CoerceValueCallback coerceValueCallback,
             bool isAnimationProhibited,
             UpdateSourceTrigger defaultUpdateSourceTrigger)
             : base(defaultValue, propertyChangedCallback, coerceValueCallback, isAnimationProhibited)
 {
     this.DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
     this.LoadFlags(flags);
 }
Example #36
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;
            }
        }
Example #37
0
        //------------------------------------------------------
        // 
        //  Internal Methods
        //
        //-----------------------------------------------------
 
        // return false if this is an invalid value for UpdateSourceTrigger
        internal static bool IsValidUpdateSourceTrigger(UpdateSourceTrigger value) 
        { 
            switch (value)
            { 
                case UpdateSourceTrigger.Default:
                case UpdateSourceTrigger.PropertyChanged:
                case UpdateSourceTrigger.LostFocus:
                case UpdateSourceTrigger.Explicit: 
                    return true;
 
                default: 
                    return false;
            } 
        }
		public FrameworkPropertyMetadata (
			object defaultValue, PropertyChangedCallback callback, FrameworkPropertyMetadataOptions options, UpdateSourceTrigger defaultUpdateSourceTrigger ) : this (defaultValue, callback, options) {
			this.DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
        }
Example #39
0
		public DesignItemBinding(string path)
		{
			this._propertyName = path;
			
			UpdateSourceTrigger = UpdateSourceTrigger.Default;
		}
        /// <summary> Convert the given UpdateSourceTrigger to BindingFlags. </summary>
        internal static BindingFlags FlagsFrom(UpdateSourceTrigger updateSourceTrigger)
        {
            switch (updateSourceTrigger)
            {
                case UpdateSourceTrigger.Default:           return BindingFlags.UpdateDefault;
                case UpdateSourceTrigger.PropertyChanged:   return BindingFlags.UpdateOnPropertyChanged;
                case UpdateSourceTrigger.LostFocus:         return BindingFlags.UpdateOnLostFocus;
                case UpdateSourceTrigger.Explicit:          return BindingFlags.UpdateExplicitly;
            }

            return BindingFlags.IllegalInput;
        }
        internal void ResolvePropertyDefaultSettings(BindingMode mode, UpdateSourceTrigger updateTrigger, FrameworkPropertyMetadata fwMetaData) 
        {
            // resolve "property-default" dataflow 
            if (mode == BindingMode.Default)
            {
                BindingFlags f = BindingFlags.OneWay;
                if (fwMetaData != null && fwMetaData.BindsTwoWayByDefault) 
                {
                    f = BindingFlags.TwoWay; 
                } 

                ChangeFlag(PrivateFlags.iPropagationMask, false); 
                ChangeFlag((PrivateFlags)f, true);

                if (TraceData.IsExtendedTraceEnabled(this, TraceDataLevel.ResolveDefaults))
                { 
                    TraceData.Trace(TraceEventType.Warning,
                                        TraceData.ResolveDefaultMode( 
                                            TraceData.Identify(this), 
                                            (f == BindingFlags.OneWay) ? BindingMode.OneWay : BindingMode.TwoWay));
                } 
            }

            Debug.Assert((_flags & PrivateFlags.iPropagationMask) != PrivateFlags.iPropDefault,
                "BindingExpression should not have Default propagation"); 

            // resolve "property-default" update trigger 
            if (updateTrigger == UpdateSourceTrigger.Default) 
            {
                UpdateSourceTrigger ust = GetDefaultUpdateSourceTrigger(fwMetaData); 

                SetUpdateSourceTrigger(ust);

                if (TraceData.IsExtendedTraceEnabled(this, TraceDataLevel.ResolveDefaults)) 
                {
                    TraceData.Trace(TraceEventType.Warning, 
                                        TraceData.ResolveDefaultUpdate( 
                                            TraceData.Identify(this),
                                            ust)); 
                }
            }

            Invariant.Assert((_flags & PrivateFlags.iUpdateMask) != PrivateFlags.iUpdateDefault, 
                "BindingExpression should not have Default update trigger");
        } 
		public FrameworkPropertyMetadata (object defaultValue, FrameworkPropertyMetadataOptions flags, PropertyChangedCallback propertyChangedCallback, CoerceValueCallback coerceValueCallback, bool isAnimationProhibited, UpdateSourceTrigger defaultUpdateSourceTrigger)
		{
		}
        public FrameworkPropertyMetadata(object defaultValue,
                                         FrameworkPropertyMetadataOptions flags,
                                         PropertyChangedCallback propertyChangedCallback,
                                         CoerceValueCallback coerceValueCallback,
                                         bool isAnimationProhibited,
                                         UpdateSourceTrigger defaultUpdateSourceTrigger) :
            base(defaultValue, propertyChangedCallback, coerceValueCallback, isAnimationProhibited)
        {
            if (!BindingOperations.IsValidUpdateSourceTrigger(defaultUpdateSourceTrigger))
                throw new InvalidEnumArgumentException("defaultUpdateSourceTrigger", (int) defaultUpdateSourceTrigger, typeof(UpdateSourceTrigger));
            if (defaultUpdateSourceTrigger == UpdateSourceTrigger.Default)
                throw new ArgumentException(SR.Get(SRID.NoDefaultUpdateSourceTrigger), "defaultUpdateSourceTrigger");

            TranslateFlags(flags);
            DefaultUpdateSourceTrigger = defaultUpdateSourceTrigger;
        }
 internal void SetUpdateSourceTrigger(UpdateSourceTrigger ust) 
 {
     ChangeFlag(PrivateFlags.iUpdateMask, false);
     ChangeFlag((PrivateFlags)BindingBase.FlagsFrom(ust), true);
 }