10 changed files with 226 additions and 71 deletions
@ -1,7 +0,0 @@ |
|||||
namespace Perspex.Markup.Xaml.Data |
|
||||
{ |
|
||||
public interface IBinding |
|
||||
{ |
|
||||
void Bind(IObservablePropertyBag instance, PerspexProperty property); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,35 @@ |
|||||
|
// 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.Subjects; |
||||
|
|
||||
|
namespace Perspex.Markup.Xaml.Data |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Defines a binding that can be created in XAML markup.
|
||||
|
/// </summary>
|
||||
|
public interface IXamlBinding |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Applies the binding to a property on an instance.
|
||||
|
/// </summary>
|
||||
|
/// <param name="instance">The target instance.</param>
|
||||
|
/// <param name="property">The target property.</param>
|
||||
|
void Bind(IObservablePropertyBag instance, PerspexProperty property); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Creates a subject that can be used to get and set the value of the binding.
|
||||
|
/// </summary>
|
||||
|
/// <param name="target">The target instance.</param>
|
||||
|
/// <param name="targetType">The type of the target property.</param>
|
||||
|
/// <param name="targetIsDataContext">
|
||||
|
/// Whether the target property is the DataContext property.
|
||||
|
/// </param>
|
||||
|
/// <returns>An <see cref="ISubject{object}"/>.</returns>
|
||||
|
ISubject<object> CreateSubject( |
||||
|
IObservablePropertyBag target, |
||||
|
Type targetType, |
||||
|
bool targetIsDataContext = false); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,53 @@ |
|||||
|
// 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.Collections.Generic; |
||||
|
using System.Globalization; |
||||
|
using System.Linq; |
||||
|
using System.Reactive.Linq; |
||||
|
using System.Reactive.Subjects; |
||||
|
using Moq; |
||||
|
using Perspex.Controls; |
||||
|
using Perspex.Markup.Xaml.Data; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Perspex.Markup.Xaml.UnitTests.Data |
||||
|
{ |
||||
|
public class MultiBindingTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async void OneWay_Binding_Should_Be_Set_Up() |
||||
|
{ |
||||
|
var source = new { A = 1, B = 2, C = 3 }; |
||||
|
var binding = new MultiBinding |
||||
|
{ |
||||
|
Converter = new ConcatConverter(), |
||||
|
Bindings = new[] |
||||
|
{ |
||||
|
new Binding { SourcePropertyPath = "A" }, |
||||
|
new Binding { SourcePropertyPath = "B" }, |
||||
|
new Binding { SourcePropertyPath = "C" }, |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
var target = new Mock<IObservablePropertyBag>(); |
||||
|
target.Setup(x => x.GetValue(Control.DataContextProperty)).Returns(source); |
||||
|
target.Setup(x => x.GetObservable(Control.DataContextProperty)).Returns( |
||||
|
Observable.Never<object>().StartWith(source)); |
||||
|
|
||||
|
var subject = binding.CreateSubject(target.Object, typeof(string)); |
||||
|
var result = await subject.Take(1); |
||||
|
|
||||
|
Assert.Equal("1,2,3", result); |
||||
|
} |
||||
|
|
||||
|
private class ConcatConverter : IMultiValueConverter |
||||
|
{ |
||||
|
public object Convert(IList<object> values, Type targetType, object parameter, CultureInfo culture) |
||||
|
{ |
||||
|
return string.Join(",", values); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue