Esempio n. 1
0
        /// <summary>
        /// Set an attached property value for an instance in the global context.
        /// </summary>
        /// <typeparam name="TOwner">Type of the instance which the value will be associated
        /// with.</typeparam>
        /// <typeparam name="TProperty">Type of the attached property value.</typeparam>
        /// <param name="instance">The instance to set attached property for.</param>
        /// <param name="attachedProperty">The attached property to set a value for.</param>
        /// <param name="value"></param>
        /// <returns></returns>
        public void SetInstanceValue <TOwner, TProperty>(TOwner instance, AttachedProperty <TOwner, TProperty> attachedProperty, TProperty value)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (attachedProperty == null)
            {
                throw new ArgumentNullException(nameof(attachedProperty));
            }

            EnsureRegistered(attachedProperty);
            using (WriteLockScope())
            {
                var store = _stores.GetOrCreateValue(instance);
                if (Equals(value, default(TProperty)))
                {
                    store.RemoveValue(attachedProperty);
                    if (store.Count == 0)
                    {
                        _stores.Remove(instance);
                    }
                }
                else
                {
                    store.SetValue(attachedProperty, value);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get an attached property value from an instance in the global context.
        /// </summary>
        /// <typeparam name="TOwner">Type of the instance which has the associated attached
        /// property value.</typeparam>
        /// <typeparam name="TProperty">Type of the attached property value.</typeparam>
        /// <param name="instance">The instance to retrieve attached property for.</param>
        /// <param name="attachedProperty">The attached property to get a value for.</param>
        /// <returns></returns>
        public TProperty GetInstanceValue <TOwner, TProperty>(TOwner instance, AttachedProperty <TOwner, TProperty> attachedProperty)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (attachedProperty == null)
            {
                throw new ArgumentNullException(nameof(attachedProperty));
            }

            EnsureRegistered(attachedProperty);
            using (ReadLockScope())
            {
                var found = _stores.TryGetValue(instance, out var store);
                if (!found)
                {
                    return(default(TProperty));
                }

                found = store.TryGetValue(attachedProperty, out var value);
                if (!found)
                {
                    return(default(TProperty));
                }
                return((TProperty)value);
            }
        }