Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Fix attached property
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Jul 26, 2023
1 parent cf16698 commit f3a9029
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/Avalonia.Xaml.Interactions/Core/ChangePropertyAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,56 @@ public class ChangePropertyAction : AvaloniaObject, IAction
private static readonly char[] s_trimChars = { '(', ')' };
private static readonly char[] s_separator = { '.' };

private static Type? GetTypeByName(string name)
{
return
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.Select(assembly => assembly.GetType(name))
.FirstOrDefault(t => t is not null)
??
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.Name == name);
}

private static AvaloniaProperty? FindAttachedProperty(object? targetObject, string propertyName)
{
if (targetObject is null)
{
return null;
}
var targetType = targetObject.GetType();
var registeredAttached = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(targetType);

var propertyNames = propertyName.Trim().Trim(s_trimChars).Split(s_separator);
if (propertyNames.Length != 2)
{
return null;
}
var targetPropertyTypeName = propertyNames[0];
var targetPropertyName = propertyNames[1];
var targetType = GetTypeByName(targetPropertyTypeName) ?? targetObject.GetType();

var registeredAttached = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(targetType);

foreach (var avaloniaProperty in registeredAttached)
{
if (avaloniaProperty.OwnerType.Name == propertyNames[0] && avaloniaProperty.Name == propertyNames[1])
if (avaloniaProperty.OwnerType.Name == targetPropertyTypeName && avaloniaProperty.Name == targetPropertyName)
{
return avaloniaProperty;
}
}

var registeredInherited = AvaloniaPropertyRegistry.Instance.GetRegisteredInherited(targetType);

foreach (var avaloniaProperty in registeredInherited)
{
if (avaloniaProperty.Name == targetPropertyName)
{
return avaloniaProperty;
}
}

return null;
}

Expand Down Expand Up @@ -110,7 +140,7 @@ public virtual object Execute(object? sender, object? parameter)

if (targetObject is AvaloniaObject avaloniaObject)
{
if (PropertyName.Contains("."))
if (PropertyName.Contains('.'))
{
var avaloniaProperty = FindAttachedProperty(targetObject, PropertyName);
if (avaloniaProperty is { })
Expand Down

0 comments on commit f3a9029

Please sign in to comment.