Ejemplo n.º 1
0
        /// <summary>
        /// Will ensure events aren't triggered when value is set
        /// </summary>
        /// <param name="propertyPath"></param>
        /// <param name="value"></param>
        internal void SetValue(string[] propertyPath, object value, BindingRegistration bindingRegistrationToSkipNotifying, PropertyInfoAndObject preObtainedSourceProperty = null)
        {
            var property = preObtainedSourceProperty ?? GetProperty(propertyPath);

            if (property != null)
            {
                BindingHost bindingHost = null;
                InternalBindingRegistration internalRegistrationToSkip = null;
                if (bindingRegistrationToSkipNotifying != null)
                {
                    internalRegistrationToSkip = bindingRegistrationToSkipNotifying.GetFinalInternalRegistration();
                    bindingHost = FindBindingHost(propertyPath);
                    if (bindingHost != null)
                    {
                        bindingHost._bindingRegistrationToSkipOnce = internalRegistrationToSkip;
                    }
                }

                try
                {
                    property.PropertyInfo.SetValue(property.Object, value);
                }
                finally
                {
                    if (bindingHost != null && bindingHost._bindingRegistrationToSkipOnce == internalRegistrationToSkip)
                    {
                        bindingHost._bindingRegistrationToSkipOnce = null;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private BindingRegistration SetBinding(string[] propertyPaths, Action action, bool skipInvokingActionImmediately, bool topLevel = true)
        {
            string immediatePath = propertyPaths[0];

            if (propertyPaths.Length == 1)
            {
                List <InternalBindingRegistration> storedBindings;
                if (!_bindings.TryGetValue(immediatePath, out storedBindings))
                {
                    storedBindings           = new List <InternalBindingRegistration>();
                    _bindings[immediatePath] = storedBindings;
                }

                var internalRegistration = new InternalBindingRegistration(action);
                storedBindings.Add(internalRegistration);

                // We require DataContext to be set here since bindings can be wired before DataContext is set
                if (DataContext != null && !skipInvokingActionImmediately)
                {
                    action();
                }

                return(new BindingRegistration(this, immediatePath, internalRegistration, topLevel ? propertyPaths : null));
            }
            else
            {
                BindingHost subBinding;
                if (!_subPropertyBindings.TryGetValue(immediatePath, out subBinding))
                {
                    subBinding = new BindingHost()
                    {
                        DataContext = GetValueFromSingleProperty(propertyPaths[0])
                    };
                    _subPropertyBindings[immediatePath] = subBinding;
                }

                var subRegistration = subBinding.SetBinding(propertyPaths.Skip(1).ToArray(), action, skipInvokingActionImmediately, topLevel: false);

                // For this we need to execute first time even if data context was null (for example binding Class.Name should execute even if Class was null)
                if (DataContext != null && subBinding.DataContext == null && !skipInvokingActionImmediately)
                {
                    action();
                }

                return(new BindingRegistration(this, immediatePath, subRegistration, topLevel ? propertyPaths : null));
            }
        }