11 changed files with 94 additions and 105 deletions
@ -0,0 +1,25 @@ |
|||
// 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.
|
|||
|
|||
namespace Perspex.Markup.Xaml.Binding |
|||
{ |
|||
public enum RelativeSourceMode |
|||
{ |
|||
DataContext, |
|||
TemplatedParent, |
|||
} |
|||
|
|||
public class RelativeSource |
|||
{ |
|||
public RelativeSource() |
|||
{ |
|||
} |
|||
|
|||
public RelativeSource(RelativeSourceMode mode) |
|||
{ |
|||
Mode = mode; |
|||
} |
|||
|
|||
public RelativeSourceMode Mode { get; set; } |
|||
} |
|||
} |
|||
@ -1,24 +1,13 @@ |
|||
// 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 Perspex.Controls; |
|||
|
|||
namespace Perspex.Markup.Xaml.Binding |
|||
{ |
|||
public class XamlBindingDefinition |
|||
{ |
|||
public XamlBindingDefinition( |
|||
string sourcePropertyPath, |
|||
BindingMode bindingMode, |
|||
bool isTemplateBinding = false) |
|||
{ |
|||
SourcePropertyPath = sourcePropertyPath; |
|||
BindingMode = bindingMode; |
|||
IsTemplateBinding = isTemplateBinding; |
|||
} |
|||
|
|||
public string SourcePropertyPath { get; } |
|||
public BindingMode BindingMode { get; } |
|||
public bool IsTemplateBinding { get; } |
|||
public BindingMode Mode { get; set; } |
|||
public BindingPriority Priority { get; set; } |
|||
public RelativeSource RelativeSource { get; set; } |
|||
public string SourcePropertyPath { get; set; } |
|||
} |
|||
} |
|||
@ -1,57 +0,0 @@ |
|||
// 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 = PerspexPropertyRegistry.Instance.FindRegistered( |
|||
templatedParent, |
|||
SourcePropertyPath); |
|||
|
|||
if (sourceProperty == null) |
|||
{ |
|||
throw new InvalidOperationException( |
|||
$"The property {SourcePropertyPath} could not be found on {templatedParent.GetType()}."); |
|||
} |
|||
|
|||
instance.Bind(targetProperty, templatedParent.GetObservable(sourceProperty), BindingPriority.Style); |
|||
templatedParent.Bind(sourceProperty, instance.GetObservable(targetProperty), BindingPriority.Style); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue