7 changed files with 147 additions and 8 deletions
@ -0,0 +1,54 @@ |
|||
// Copyright (c) The Perspex 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.Linq; |
|||
using OmniXaml.TypeConversion; |
|||
using Perspex.Controls; |
|||
using Perspex.Styling; |
|||
|
|||
namespace Perspex.Markup.Xaml.Binding |
|||
{ |
|||
public class XamlTemplateBinding |
|||
{ |
|||
private readonly ITypeConverterProvider _typeConverterProvider; |
|||
|
|||
public XamlTemplateBinding() |
|||
{ |
|||
} |
|||
|
|||
public XamlTemplateBinding(ITypeConverterProvider typeConverterProvider) |
|||
{ |
|||
_typeConverterProvider = typeConverterProvider; |
|||
} |
|||
|
|||
public string SourcePropertyPath { get; set; } |
|||
|
|||
public BindingMode BindingMode { get; set; } |
|||
|
|||
public void Bind(PerspexObject instance, PerspexProperty targetProperty) |
|||
{ |
|||
instance.GetObservable(Control.TemplatedParentProperty) |
|||
.Where(x => x != null) |
|||
.OfType<PerspexObject>() |
|||
.Take(1) |
|||
.Subscribe(x => BindToTemplatedParent(instance, targetProperty, x)); |
|||
} |
|||
|
|||
private void BindToTemplatedParent( |
|||
PerspexObject instance, |
|||
PerspexProperty targetProperty, |
|||
PerspexObject templatedParent) |
|||
{ |
|||
var sourceProperty = templatedParent.FindRegistered(SourcePropertyPath); |
|||
|
|||
if (sourceProperty == null) |
|||
{ |
|||
throw new InvalidOperationException( |
|||
$"The property {SourcePropertyPath} could not be found on {instance.GetType()}."); |
|||
} |
|||
|
|||
instance.Bind(targetProperty, templatedParent.GetObservable(sourceProperty), BindingPriority.Style); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using OmniXaml; |
|||
using Perspex.Markup.Xaml.Binding; |
|||
|
|||
namespace Perspex.Markup.Xaml.MarkupExtensions |
|||
{ |
|||
public class TemplateBindingExtension : MarkupExtension |
|||
{ |
|||
public TemplateBindingExtension() |
|||
{ |
|||
} |
|||
|
|||
public TemplateBindingExtension(string path) |
|||
{ |
|||
Path = path; |
|||
} |
|||
|
|||
public override object ProvideValue(MarkupExtensionContext extensionContext) |
|||
{ |
|||
return new XamlBindingDefinition(Path, Mode, true); |
|||
} |
|||
|
|||
public string Path { get; set; } |
|||
public BindingMode Mode { get; set; } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue