/// <summary> /// Clears the value of the property to the default value. /// </summary> /// <param name="propertyKey">The <see cref="DependencyPropertyKey"/> of the <see cref="DependencyProperty"/> which should be cleared.</param> public void ClearValue(DependencyPropertyKey propertyKey) { if (propertyKey == null) { throw new ArgumentNullException(nameof(propertyKey)); } DependencyProperty property = propertyKey.Property; PropertyValueStorage storage = GetStorage(property, false, false); if (storage != null) { // Get the default value of the property Object defaultValue = property.GetPropertyDefault(RepresentedObject); // Set the default value to trigger events storage.SetValue(this, defaultValue); // Check if the storage is still existing and can be removed ( to keep the object small ) // TODO : This could be much faster when using something else than a dictionary if (storage.IsRemoveable && storage.BaseValue == defaultValue && _Values.TryGetValue(property, out PropertyValueStorage currentStorage) && currentStorage == storage) { // Try to remove the property from the container _Values.Remove(property); } } }
/// <summary> /// Coerces the value of the specified property. /// </summary> /// <param name="property">The <see cref="DependencyProperty"/> to coerce.</param> public void CoerceValue(DependencyProperty property) { PropertyValueStorage storage = GetStorage(property, true, false); // In any case we need to coerce the property value storage.CoerceValue(this); // Check if we can remove the storage again if (storage.IsRemoveable && Equals(storage.BaseValue, property.GetPropertyDefault(RepresentedObject)) && Equals(storage.Value, storage.BaseValue) && _Values.TryGetValue(property, out PropertyValueStorage currentStorage) && currentStorage == storage) { // Remove the property again _Values.Remove(property); } }
/// <summary> /// Gets the base value of the specified property. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="property">The <see cref="DependencyProperty{TPropertyType}"/> to get the base value for.</param> /// <returns>The base value of the property.</returns> public TProperty GetBaseValue <TProperty>(DependencyProperty <TProperty> property) { PropertyValueStorage <TProperty> storage = GetStorage(property, false, false); if (storage == null) { if (property.IsUsingValueFactory) { storage = GetStorage(property, true, false); } else { return(property.GetPropertyDefault(null)); } } return(storage.BaseValue); }
/// <summary> /// Gets the value of the dependency property. /// </summary> /// <param name="property">The <see cref="DependencyProperty"/> to get the value for.</param> /// <returns>The value of the property.</returns> public Object GetValue(DependencyProperty property) { PropertyValueStorage storage = GetStorage(property, false, false); if (storage == null) { if (property.IsUsingValueFactory) { storage = GetStorage(property, true, false); } else { return(property.GetPropertyDefault(RepresentedObject)); } } return(storage.Value); }