AttachedProperty.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. namespace Perspex
  5. {
  6. /// <summary>
  7. /// An attached perspex property.
  8. /// </summary>
  9. /// <typeparam name="TValue">The type of the property's value.</typeparam>
  10. public class AttachedProperty<TValue> : StyledPropertyBase<TValue>
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the <see cref="AttachedProperty{TValue}"/> class.
  14. /// </summary>
  15. /// <param name="name">The name of the property.</param>
  16. /// <param name="ownerType">The class that is registering the property.</param>
  17. /// <param name="metadata">The property metadata.</param>
  18. /// <param name="inherits">Whether the property inherits its value.</param>
  19. public AttachedProperty(
  20. string name,
  21. Type ownerType,
  22. StyledPropertyMetadata<TValue> metadata,
  23. bool inherits = false)
  24. : base(name, ownerType, metadata, inherits)
  25. {
  26. }
  27. /// <inheritdoc/>
  28. public override bool IsAttached => true;
  29. /// <summary>
  30. /// Attaches the property as a non-attached property on the specified type.
  31. /// </summary>
  32. /// <typeparam name="TOwner">The owner type.</typeparam>
  33. /// <returns>The property.</returns>
  34. public StyledProperty<TValue> AddOwner<TOwner>() where TOwner : IPerspexObject
  35. {
  36. var result = new StyledProperty<TValue>(this, typeof(TOwner));
  37. PerspexPropertyRegistry.Instance.Register(typeof(TOwner), result);
  38. return result;
  39. }
  40. }
  41. }