diff --git a/DialogHost.Avalonia/Utilities/MultiDynamicResourceExtension.cs b/DialogHost.Avalonia/Utilities/MultiDynamicResourceExtension.cs index 81ecd45..c005eff 100644 --- a/DialogHost.Avalonia/Utilities/MultiDynamicResourceExtension.cs +++ b/DialogHost.Avalonia/Utilities/MultiDynamicResourceExtension.cs @@ -3,58 +3,47 @@ using System.Linq; using Avalonia; using Avalonia.Controls; -using Avalonia.Data; +using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml.Converters; using Avalonia.Media; +using Avalonia.Styling; namespace DialogHostAvalonia.Utilities { /// - /// Allows to bind to several different DynamicResources + /// Allows binding to several different DynamicResources /// - internal class MultiDynamicResourceExtension : Binding, IBinding + // TODO: Replace it with proper implementation when https://github.com/AvaloniaUI/Avalonia/issues/15270 will be resolved + internal class MultiDynamicResourceExtension : MarkupExtension { public IBrush DefaultBrush { get; set; } public object ResourceKeys { get; set; } - public IBinding ProvideValue(IServiceProvider serviceProvider) + public override object ProvideValue(IServiceProvider serviceProvider) { - return this; - } - - InstancedBinding? IBinding.Initiate( - AvaloniaObject target, - AvaloniaProperty? targetProperty, - object? anchor, - bool enableDataValidation) - { - if (ResourceKeys is not string resourceKey) + var provideValueTarget = serviceProvider.GetService(); + if (provideValueTarget is null) { - return null; + throw new InvalidOperationException( + "Can't resolve IProvideValueTarget from Avalonia service provider."); } - var resourceKeys = resourceKey.Split(';'); - - var control = target as IResourceHost ?? DefaultAnchor?.Target as IResourceHost; + var setter = (Setter)provideValueTarget.TargetObject; + var targetProperty = setter.Property; - if (control != null) - { - var source = - resourceKeys.Select(key => control.GetResourceObservable(key, GetConverter(targetProperty))); - var testObservable = new MultiDynamicResourceObservable(source, DefaultBrush); - return InstancedBinding.OneWay(testObservable, Priority); - } - - if (DefaultAnchor?.Target is IResourceProvider resourceProvider) + if (ResourceKeys is not string resourceKey) { - var source = resourceKeys.Select(key => - resourceProvider.GetResourceObservable(key, GetConverter(targetProperty))); - var testObservable = new MultiDynamicResourceObservable(source, DefaultBrush); - return InstancedBinding.OneWay(testObservable, Priority); + throw new InvalidOperationException( + "ResourceKeys should be string with ; delimeter"); } - return null; + var resourceKeys = resourceKey.Split(';'); + var source = resourceKeys + .Select(key => Application.Current! + .GetResourceObservable(key, GetConverter(targetProperty))); + var testObservable = new MultiDynamicResourceObservable(source, DefaultBrush); + return testObservable.ToBinding(); } private static Func? GetConverter(AvaloniaProperty? targetProperty)