ChangeAvaloniaPropertyAction.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Avalonia.Controls;
  6. using Avalonia.Xaml.Interactivity;
  7. namespace Avalonia.Xaml.Interactions.Custom;
  8. /// <summary>
  9. /// An action that will change a specified Avalonia property to a specified value when invoked.
  10. /// </summary>
  11. [RequiresUnreferencedCode("This functionality is not compatible with trimming.")]
  12. public class ChangeAvaloniaPropertyAction : Avalonia.Xaml.Interactivity.Action
  13. {
  14. /// <summary>
  15. /// Identifies the <seealso cref="TargetProperty"/> avalonia property.
  16. /// </summary>
  17. public static readonly StyledProperty<AvaloniaProperty?> TargetPropertyProperty =
  18. AvaloniaProperty.Register<ChangeAvaloniaPropertyAction, AvaloniaProperty?>(nameof(TargetProperty));
  19. /// <summary>
  20. /// Identifies the <seealso cref="TargetObject"/> avalonia property.
  21. /// </summary>
  22. public static readonly StyledProperty<AvaloniaObject?> TargetObjectProperty =
  23. AvaloniaProperty.Register<ChangeAvaloniaPropertyAction, AvaloniaObject?>(nameof(TargetObject));
  24. /// <summary>
  25. /// Identifies the <seealso cref="Value"/> avalonia property.
  26. /// </summary>
  27. public static readonly StyledProperty<object?> ValueProperty =
  28. AvaloniaProperty.Register<ChangeAvaloniaPropertyAction, object?>(nameof(Value));
  29. /// <summary>
  30. /// Gets or sets the name of the Avalonia property to change. This is a avalonia property.
  31. /// </summary>
  32. public AvaloniaProperty? TargetProperty
  33. {
  34. get => GetValue(TargetPropertyProperty);
  35. set => SetValue(TargetPropertyProperty, value);
  36. }
  37. /// <summary>
  38. /// Gets or sets the value to set. This is a avalonia property.
  39. /// </summary>
  40. public object? Value
  41. {
  42. get => GetValue(ValueProperty);
  43. set => SetValue(ValueProperty, value);
  44. }
  45. /// <summary>
  46. /// Gets or sets the Avalonia object whose property will be changed.
  47. /// If <seealso cref="TargetObject"/> is not set or cannot be resolved, the sender of <seealso cref="Execute"/> will be used. This is a avalonia property.
  48. /// </summary>
  49. [ResolveByName]
  50. public AvaloniaObject? TargetObject
  51. {
  52. get => GetValue(TargetObjectProperty);
  53. set => SetValue(TargetObjectProperty, value);
  54. }
  55. /// <summary>
  56. /// Executes the action.
  57. /// </summary>
  58. /// <param name="sender">The <see cref="object"/> that is passed to the action by the behavior. Generally this is <seealso cref="IBehavior.AssociatedObject"/> or a target object.</param>
  59. /// <param name="parameter">The value of this parameter is determined by the caller.</param>
  60. /// <returns>True if updating the property value succeeds; else false.</returns>
  61. public override object Execute(object? sender, object? parameter)
  62. {
  63. if (!IsEnabled)
  64. {
  65. return false;
  66. }
  67. var targetObject = GetValue(TargetObjectProperty) is not null ? TargetObject : sender;
  68. if (targetObject is AvaloniaObject avaloniaObject && TargetProperty is not null)
  69. {
  70. UpdateAvaloniaPropertyValue(avaloniaObject, TargetProperty);
  71. return true;
  72. }
  73. return false;
  74. }
  75. private void UpdateAvaloniaPropertyValue(AvaloniaObject targetObject, AvaloniaProperty targetProperty)
  76. {
  77. ValidateTargetProperty(targetProperty);
  78. Exception? innerException = null;
  79. try
  80. {
  81. object? result = null;
  82. var propertyType = targetProperty.PropertyType;
  83. var propertyTypeInfo = propertyType.GetTypeInfo();
  84. if (Value is null)
  85. {
  86. // The result can be null if the type is generic (nullable), or the default value of the type in question
  87. result = propertyTypeInfo.IsValueType ? Activator.CreateInstance(propertyType) : null;
  88. }
  89. else if (propertyTypeInfo.IsAssignableFrom(Value.GetType().GetTypeInfo()))
  90. {
  91. result = Value;
  92. }
  93. else
  94. {
  95. var valueAsString = Value.ToString();
  96. if (valueAsString is not null)
  97. {
  98. result = propertyTypeInfo.IsEnum
  99. ? Enum.Parse(propertyType, valueAsString, false)
  100. : TypeConverterHelper.Convert(valueAsString, propertyType);
  101. }
  102. }
  103. targetObject.SetValue(targetProperty, result);
  104. }
  105. catch (FormatException e)
  106. {
  107. innerException = e;
  108. }
  109. catch (ArgumentException e)
  110. {
  111. innerException = e;
  112. }
  113. if (innerException is not null)
  114. {
  115. throw new ArgumentException(string.Format(
  116. CultureInfo.CurrentCulture,
  117. "Cannot assign value of type {0} to property {1} of type {2}. The {1} property can be assigned only values of type {2}.",
  118. Value?.GetType().Name ?? "null",
  119. targetProperty.Name,
  120. targetObject.GetType().Name),
  121. innerException);
  122. }
  123. }
  124. /// <summary>
  125. /// Ensures the property is not null and can be written to.
  126. /// </summary>
  127. private void ValidateTargetProperty(AvaloniaProperty? targetProperty)
  128. {
  129. if (targetProperty is null)
  130. {
  131. throw new ArgumentException(nameof(TargetProperty));
  132. }
  133. else if (targetProperty.IsReadOnly)
  134. {
  135. throw new ArgumentException(string.Format(
  136. CultureInfo.CurrentCulture,
  137. "Property {0} is read-only.",
  138. targetProperty.Name));
  139. }
  140. }
  141. }