Skip to content

Commit

Permalink
Add a workaround for binding refactor #48
Browse files Browse the repository at this point in the history
  • Loading branch information
SKProCH committed Aug 21, 2024
1 parent 82ffd15 commit 8f1b4cc
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions DialogHost.Avalonia/Utilities/MultiDynamicResourceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Allows to bind to several different DynamicResources
/// Allows binding to several different DynamicResources
/// </summary>
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<IProvideValueTarget>();
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<object?, object?>? GetConverter(AvaloniaProperty? targetProperty)
Expand Down

0 comments on commit 8f1b4cc

Please sign in to comment.