Exemplo n.º 1
0
        /// <summary>
        /// Sets a <see cref="PerspexProperty"/> value.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        /// <param name="priority">The priority of the value.</param>
        public void SetValue(
            PerspexProperty property,
            object value,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            Contract.Requires <ArgumentNullException>(property != null);
            VerifyAccess();

            if (property.IsDirect)
            {
                property = GetRegistered(property);

                if (property.Setter == null)
                {
                    throw new ArgumentException($"The property {property.Name} is readonly.");
                }

                LogPropertySet(property, value, priority);
                property.Setter(this, UnsetToDefault(value, property));
            }
            else
            {
                PriorityValue v;
                var           originalValue = value;

                if (!PerspexPropertyRegistry.Instance.IsRegistered(this, property))
                {
                    ThrowNotRegistered(property);
                }

                if (!TypeUtilities.TryCast(property.PropertyType, value, out value))
                {
                    throw new ArgumentException(string.Format(
                                                    "Invalid value for Property '{0}': '{1}' ({2})",
                                                    property.Name,
                                                    originalValue,
                                                    originalValue?.GetType().FullName ?? "(null)"));
                }

                if (!_values.TryGetValue(property, out v))
                {
                    if (value == PerspexProperty.UnsetValue)
                    {
                        return;
                    }

                    v = CreatePriorityValue(property);
                    _values.Add(property, v);
                }

                LogPropertySet(property, value, priority);
                v.SetValue(value, (int)priority);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sets a <see cref="PerspexProperty"/> value.
        /// </summary>
        /// <typeparam name="T">The type of the property.</typeparam>
        /// <param name="property">The property.</param>
        /// <param name="value">The value.</param>
        /// <param name="priority">The priority of the value.</param>
        public void SetValue <T>(
            PerspexProperty <T> property,
            T value,
            BindingPriority priority = BindingPriority.LocalValue)
        {
            Contract.Requires <ArgumentNullException>(property != null);
            VerifyAccess();
            if (property.IsDirect)
            {
                property = (PerspexProperty <T>)GetRegistered(property);

                if (property.Setter == null)
                {
                    throw new ArgumentException($"The property {property.Name} is readonly.");
                }

                LogPropertySet(property, value, priority);
                property.Setter(this, value);
            }
            else
            {
                SetValue((PerspexProperty)property, value, priority);
            }
        }