committed by
GitHub
9 changed files with 34 additions and 359 deletions
@ -1,69 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using System.Reactive.Disposables; |
|||
using System.Reactive.Linq; |
|||
using System.Reactive.Subjects; |
|||
using Avalonia.Controls; |
|||
using Avalonia.Data; |
|||
using Avalonia.Styling; |
|||
|
|||
namespace Avalonia.Markup.Xaml.Data |
|||
{ |
|||
public class StyleResourceBinding : IBinding |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="StyleResourceBinding"/> class.
|
|||
/// </summary>
|
|||
/// <param name="name">The resource name.</param>
|
|||
public StyleResourceBinding(string name) |
|||
{ |
|||
Name = name; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public BindingMode Mode => BindingMode.OneTime; |
|||
|
|||
/// <summary>
|
|||
/// Gets the resource name.
|
|||
/// </summary>
|
|||
public string Name { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
public BindingPriority Priority => BindingPriority.LocalValue; |
|||
|
|||
/// <inheritdoc/>
|
|||
public InstancedBinding Initiate( |
|||
IAvaloniaObject target, |
|||
AvaloniaProperty targetProperty, |
|||
object anchor = null, |
|||
bool enableDataValidation = false) |
|||
{ |
|||
var host = (target as IControl) ?? (anchor as IControl); |
|||
var style = anchor as IStyle; |
|||
var resource = AvaloniaProperty.UnsetValue; |
|||
|
|||
if (host != null) |
|||
{ |
|||
resource = host.FindResource(Name); |
|||
} |
|||
else if (style != null) |
|||
{ |
|||
if (!style.TryGetResource(Name, out resource)) |
|||
{ |
|||
resource = AvaloniaProperty.UnsetValue; |
|||
} |
|||
} |
|||
|
|||
if (resource != AvaloniaProperty.UnsetValue) |
|||
{ |
|||
return new InstancedBinding(resource, Priority); |
|||
} |
|||
else |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,31 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using Avalonia.Data; |
|||
using Avalonia.Markup.Xaml.Data; |
|||
using System; |
|||
|
|||
namespace Avalonia.Markup.Xaml.MarkupExtensions |
|||
{ |
|||
using Portable.Xaml.Markup; |
|||
using PortableXaml; |
|||
|
|||
[MarkupExtensionReturnType(typeof(IBinding))] |
|||
public class StyleResourceExtension : MarkupExtension |
|||
{ |
|||
public StyleResourceExtension(string name) |
|||
{ |
|||
Name = name; |
|||
} |
|||
|
|||
public override object ProvideValue(IServiceProvider serviceProvider) |
|||
{ |
|||
return XamlBinding.FromMarkupExtensionContext( |
|||
new StyleResourceBinding(Name), |
|||
serviceProvider); |
|||
} |
|||
|
|||
[ConstructorArgument("name")] |
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
@ -1,63 +0,0 @@ |
|||
using Avalonia.Controls; |
|||
using Avalonia.Data; |
|||
using Avalonia.Styling; |
|||
using Portable.Xaml; |
|||
using Portable.Xaml.ComponentModel; |
|||
using System.ComponentModel; |
|||
using Portable.Xaml.Markup; |
|||
using System; |
|||
|
|||
namespace Avalonia.Markup.Xaml.PortableXaml |
|||
{ |
|||
internal class XamlBinding : IBinding |
|||
{ |
|||
public static IBinding FromMarkupExtensionContext( |
|||
IBinding binding, |
|||
IServiceProvider serviceProvider) |
|||
{ |
|||
var context = (ITypeDescriptorContext)serviceProvider; |
|||
var pvt = context.GetService<IProvideValueTarget>(); |
|||
|
|||
if (pvt.TargetObject is IControl) return binding; |
|||
|
|||
object anchor = GetDefaultAnchor(context); |
|||
|
|||
if (anchor == null) return binding; |
|||
|
|||
return new XamlBinding(binding, anchor); |
|||
} |
|||
|
|||
private static object GetDefaultAnchor(ITypeDescriptorContext context) |
|||
{ |
|||
object anchor = null; |
|||
|
|||
// The target is not a control, so we need to find an anchor that will let us look
|
|||
// up named controls and style resources. First look for the closest IControl in
|
|||
// the context.
|
|||
anchor = context.GetFirstAmbientValue<IControl>(); |
|||
|
|||
// If a control was not found, then try to find the highest-level style as the XAML
|
|||
// file could be a XAML file containing only styles.
|
|||
return anchor ?? |
|||
context.GetService<IRootObjectProvider>()?.RootObject as IStyle ?? |
|||
context.GetLastOrDefaultAmbientValue<IStyle>(); |
|||
} |
|||
|
|||
private XamlBinding(IBinding binding, object anchor) |
|||
{ |
|||
Value = binding; |
|||
|
|||
Anchor = new WeakReference(anchor); |
|||
} |
|||
|
|||
public WeakReference Anchor { get; } |
|||
|
|||
public IBinding Value { get; } |
|||
|
|||
public InstancedBinding Initiate(IAvaloniaObject target, AvaloniaProperty targetProperty, object anchor = null, bool enableDataValidation = false) |
|||
{ |
|||
return Value.Initiate(target, targetProperty, |
|||
anchor ?? Anchor.Target, enableDataValidation); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue