using System;
using Avalonia.Data.Converters;
using Avalonia.LogicalTree;
using Avalonia.Reactive;
using Avalonia.Styling;
#nullable enable
namespace Avalonia.Controls
{
public static class ResourceNodeExtensions
{
///
/// Finds the specified resource by searching up the logical tree and then global styles.
///
/// The control.
/// The resource key.
/// The resource, or if not found.
public static object? FindResource(this IResourceHost control, object key)
{
control = control ?? throw new ArgumentNullException(nameof(control));
key = key ?? throw new ArgumentNullException(nameof(key));
if (control.TryFindResource(key, out var value))
{
return value;
}
return AvaloniaProperty.UnsetValue;
}
///
/// Tries to the specified resource by searching up the logical tree and then global styles.
///
/// The control.
/// The resource key.
/// On return, contains the resource if found, otherwise null.
/// True if the resource was found; otherwise false.
public static bool TryFindResource(this IResourceHost control, object key, out object? value)
{
control = control ?? throw new ArgumentNullException(nameof(control));
key = key ?? throw new ArgumentNullException(nameof(key));
IResourceNode? current = control;
while (current != null)
{
if (current.TryGetResource(key, out value))
{
return true;
}
current = (current as IStyleHost)?.StylingParent as IResourceNode;
}
value = null;
return false;
}
public static IObservable