/// <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); } } }
/// <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); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="instance"></param> /// <param name="attachedProperty"></param> /// <param name="value"></param> /// <param name="context"></param> public static void SetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, TResult value, AttachedPropertyContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } context.SetInstanceValue(instance, attachedProperty, value); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="instance"></param> /// <param name="attachedProperty"></param> /// <param name="context"></param> /// <returns></returns> public static TResult GetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, AttachedPropertyContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var value = context.GetInstanceValue(instance, attachedProperty); return(value); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="instance"></param> /// <param name="attachedProperty"></param> /// <param name="value"></param> public static void SetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty, TResult value) { instance.SetAttachedValue(attachedProperty, value, AttachedPropertyContext.GlobalContext); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="instance"></param> /// <param name="attachedProperty"></param> /// <returns></returns> public static TResult GetAttachedValue <T, TResult>(this T instance, AttachedProperty <T, TResult> attachedProperty) { var value = instance.GetAttachedValue(attachedProperty, AttachedPropertyContext.GlobalContext); return(value); }