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.
|
// 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.
|
// 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 |
namespace Perspex.Markup.Xaml.Binding |
||||
{ |
{ |
||||
public class XamlBindingDefinition |
public class XamlBindingDefinition |
||||
{ |
{ |
||||
public XamlBindingDefinition( |
public BindingMode Mode { get; set; } |
||||
string sourcePropertyPath, |
public BindingPriority Priority { get; set; } |
||||
BindingMode bindingMode, |
public RelativeSource RelativeSource { get; set; } |
||||
bool isTemplateBinding = false) |
public string SourcePropertyPath { get; set; } |
||||
{ |
|
||||
SourcePropertyPath = sourcePropertyPath; |
|
||||
BindingMode = bindingMode; |
|
||||
IsTemplateBinding = isTemplateBinding; |
|
||||
} |
|
||||
|
|
||||
public string SourcePropertyPath { get; } |
|
||||
public BindingMode BindingMode { get; } |
|
||||
public bool IsTemplateBinding { get; } |
|
||||
} |
} |
||||
} |
} |
||||
@ -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