public void NestedCollectionAbsolutePathTest() { /*Arrange*/ //create a parent binding. /*Arrange*/ Binding.BindingDef parent = new Binding.BindingDef(new WebformControl(new TextBox()), "", new Options { Path = "" }, controlService); string rawPath = "ViewModelID"; BindingPath path = new BindingPath(rawPath, parent.SourceExpression, PathMode.Absolute); var viewModel = new { ViewModelID = "viewmodel1" }; /*Act*/ SourceProperty sourceProperty = path.ResolveAsSource(viewModel); /*Assert*/ Assert.IsNotNull(sourceProperty); Assert.IsNotNull(sourceProperty.Descriptor); Assert.IsNotNull(sourceProperty.OwningInstance); Assert.IsNotNull(sourceProperty.Value); Assert.AreEqual("ViewModelID", sourceProperty.Descriptor.Name); Assert.AreEqual(viewModel, sourceProperty.OwningInstance); Assert.AreEqual(viewModel.ViewModelID, sourceProperty.Value); }
public void ProviderShouldReturnMultiPathObserverMultiPath() { var target = new BindingSourceModel(); var observerProvider = Cretate(); observerProvider.Observe(target, BindingPath.Create(GetMemberPath(target, model => model.NestedModel.IntProperty)), false).ShouldBeType <MultiPathObserver>(); }
protected override void OnApplyTemplate() { base.OnApplyTemplate(); _textBox = GetTemplateChild("TextBox") as TextBox; _popup = GetTemplateChild("SuggestionsPopup") as Popup; _layoutRoot = GetTemplateChild("LayoutRoot") as Grid; _suggestionsList = GetTemplateChild("SuggestionsList") as ListView; _queryButton = GetTemplateChild("QueryButton") as Button; #if __ANDROID__ _popup.DisableFocus(); #endif UpdateQueryButton(); UpdateTextBox(); _textBoxBinding = new BindingPath("Text", null) { DataContext = _textBox, ValueChangedListener = this }; Loaded += (s, e) => RegisterEvents(); Unloaded += (s, e) => UnregisterEvents(); if (IsLoaded) { RegisterEvents(); } }
public void SimplePathTargetResolutionTest() { string rawPath = "tb1.Text"; BindingPath path = new BindingPath(rawPath); TextBox tb = new TextBox(); tb.ID = "tb1"; tb.Text = "hello"; //tell the control service to return our control when asked controlService.Expect(cs => cs.FindControlUnique(null, null)).IgnoreArguments() .Do(new Func <IBindingTarget, string, IBindingTarget>((s, e) => new WebformControl(tb))); //and unwrap controlService.Expect(cs => cs.Unwrap(null)).IgnoreArguments().Do(new Func <IBindingTarget, object>(b => tb)); TargetProperty targetProperty = path.ResolveAsTarget(new WebformControl(tb), this.controlService); Assert.IsNotNull(targetProperty); Assert.IsNotNull(targetProperty.Descriptor); Assert.IsNotNull(targetProperty.OwningControlRaw); Assert.IsNotNull(targetProperty.Value); Assert.AreEqual("hello", targetProperty.Value); Assert.AreEqual("Text", targetProperty.Descriptor.Name); Assert.AreEqual(tb, targetProperty.OwningControlRaw); }
public virtual void BindingShouldRaiseEventWhenUpdateSourceFalse() { bool isInvoked = false; IBindingPath path = new BindingPath("test"); var bindingManager = new BindingManager(); var target = new BindingSourceAccessorMock { Source = new ObserverMock { GetActualSource = b => new object(), Path = path } }; var source = new BindingSourceAccessorMock(); DataBinding binding = CreateDataBinding(target, source, bindingManager); source.SetValue = (func, context, arg3) => false; binding.BindingUpdated += (sender, args) => { args.Action.ShouldEqual(BindingAction.UpdateSource); args.Result.ShouldBeFalse(); isInvoked = true; }; binding.UpdateSource(); isInvoked.ShouldBeTrue(); }
public virtual void BindingShouldRaiseExceptionEventWhenUpdateTargetThrowException() { bool isInvoked = false; IBindingPath path = new BindingPath("test"); var bindingManager = new BindingManager(); var target = new BindingSourceAccessorMock { Source = new ObserverMock { GetActualSource = b => new object(), Path = path } }; var source = new BindingSourceAccessorMock { Source = new ObserverMock { GetActualSource = b => new object(), Path = path, IsValid = b => true } }; DataBinding binding = CreateDataBinding(target, source, bindingManager); target.SetValue = (func, context, arg3) => { throw new TestException(); }; binding.BindingUpdated += (sender, args) => { args.Action.ShouldEqual(BindingAction.UpdateTarget); args.Exception.InnerException.ShouldBeType <TestException>(); isInvoked = true; }; binding.UpdateTarget(); isInvoked.ShouldBeTrue(); }
public virtual void BindingShouldCallAttachDetachMethodInBehavior() { IBindingPath path = new BindingPath("test"); var bindingManager = new BindingManager(); var target = new BindingSourceAccessorMock { Source = new ObserverMock { GetActualSource = b => new object(), Path = path } }; var source = new BindingSourceAccessorMock(); DataBinding binding = CreateDataBinding(target, source, bindingManager); int countAttach = 0; int countDetach = 0; var first = new BindingBehaviorMock { Id = Guid.Empty, Attach = binding1 => { countAttach++; return(true); }, Detach = binding1 => countDetach++ }; binding.Behaviors.Add(first); countAttach.ShouldEqual(1); countDetach.ShouldEqual(0); binding.Behaviors.Remove(first); countAttach.ShouldEqual(1); countDetach.ShouldEqual(1); }
public virtual void BindingShouldUpdateTargetWithBindingContext() { bool isInvoked = false; IBindingPath path = new BindingPath("test"); var bindingManager = new BindingManager(); var target = new BindingSourceAccessorMock { Source = new ObserverMock { GetActualSource = b => new object(), Path = path } }; var source = new BindingSourceAccessorMock { Source = new ObserverMock { IsValid = b => true } }; DataBinding binding = CreateDataBinding(target, source, bindingManager); target.SetValue = (func, context, arg3) => { context.ShouldEqual(binding.Context); arg3.ShouldBeTrue(); isInvoked = true; return(true); }; binding.UpdateTarget(); isInvoked.ShouldBeTrue(); }
public void IndexedPathSourceResolutionTest() { string rawPath = "AvailableEmployees.Name"; BindingPath path = new BindingPath(rawPath, 1); var employee1 = new { Name = "Sam" }; var employee2 = new { Name = "Ben" }; var employee3 = new { Name = "Ali" }; var list = new[] { employee1, employee2, employee3 }; var viewModel = new { AvailableEmployees = list }; SourceProperty sourceProperty = path.ResolveAsSource(viewModel); Assert.IsNotNull(sourceProperty); Assert.IsNotNull(sourceProperty.Descriptor); Assert.IsNotNull(sourceProperty.OwningInstance); Assert.IsNotNull(sourceProperty.Value); Assert.AreEqual("Ben", sourceProperty.Value); Assert.AreEqual("Name", sourceProperty.Descriptor.Name); //empoyee at the index of 1 Assert.AreEqual(employee2, sourceProperty.OwningInstance); Assert.AreEqual(employee2.Name, sourceProperty.Value); Assert.AreEqual("Name", sourceProperty.Descriptor.Name); }
private IExpressionNode VisitExpression(IExpressionNode node) { var nodes = new List <IExpressionNode>(); string memberName = node.TryGetMemberName(true, true, nodes); if (memberName == null) { _isMulti = true; return(node); } if (nodes[0] is ResourceExpressionNode) { return(GetResourceMember(node, memberName, nodes)); } IBindingPath path = BindingPath.Create(memberName); if (path.IsEmpty) { return(GetOrAddBindingMember(memberName, (s, i) => new BindingMemberExpressionNode(memberName, s, i))); } string firstMember = path.Parts[0]; if (_lamdaParameters.Contains(firstMember)) { return(node); } return(GetOrAddBindingMember(memberName, (s, i) => new BindingMemberExpressionNode(memberName, s, i))); }
/// <summary> /// Is called by the managing DataNode when an update is broadcasted. /// </summary> /// <param name="updatedPath">Path that received a value update.</param> public void OnUpdate(Path updatedPath) { // Check if this does influence this binding if (BindingPath.IsSamePath(updatedPath) || BindingPath.IsSubpathOf(updatedPath)) { ForceUpdate(); } }
public BindingContextSource(object source, IBindingMemberInfo member) { _member = member; _observer = BindingServiceProvider .ObserverProvider .Observe(source, BindingPath.Create(member.Path), true); _observer.Listener = this; }
public void SourceShouldReturnPathFromObserver() { var mock = new ObserverMock(); BindingSource target = CreateBindingSource(mock); target.Path.ShouldBeNull(); mock.Path = BindingPath.Create("test"); target.Path.ShouldEqual(mock.Path); }
private IExpressionNode GetResourceMember(IExpressionNode node, string memberName, IList <IExpressionNode> nodes) { IExpressionNode staticValue; if (_staticNodes.TryGetValue(node, out staticValue)) { return(staticValue); } IBindingPath path = BindingPath.Create(memberName); string firstMember = path.Parts[0]; Type type = BindingServiceProvider.ResourceResolver.ResolveType(firstMember, DataContext.Empty, false); var dynMember = (ResourceExpressionNode)nodes[0]; if (dynMember.Dynamic && type == null) { memberName = BindingExtensions.MergePath(path.Parts.Skip(1).ToArray()); return(GetOrAddBindingMember("$" + path.Path, (s, i) => new BindingMemberExpressionNode(firstMember, memberName, s, i))); } bool dynamicMember = false; IExpressionNode firstMemberNode = nodes[1]; if (!_staticNodes.TryGetValue(firstMemberNode, out staticValue)) { if (type == null) { IBindingResourceObject resourceObject = BindingServiceProvider .ResourceResolver .ResolveObject(firstMember, DataContext.Empty, true); var dynamicObject = resourceObject.Value as IDynamicObject; if (dynamicObject == null || path.Parts.Count <= 1) { staticValue = new ConstantExpressionNode(resourceObject.Value, resourceObject.Type); } else { staticValue = new ConstantExpressionNode(dynamicObject.GetMember(path.Parts[1], Empty.Array <object>())); dynamicMember = true; } } else { staticValue = new ConstantExpressionNode(type, typeof(Type)); } _staticNodes[firstMemberNode] = staticValue; if (dynamicMember) { _staticNodes[nodes[2]] = staticValue; } } if (firstMemberNode == node || (dynamicMember && node == nodes[2])) { return(staticValue); } return(node); }
public void When_Parse_Indexer() { var sut = new BindingPath("[hello_world]", null); var result = sut.GetPathItems().ToArray(); result.Length.Should().Be(1); result[0].PropertyName.Should().Be("[hello_world]"); }
public void When_Parse_AttachedProperty() { var sut = new BindingPath("(Grid.Column)", null); var result = sut.GetPathItems().ToArray(); result.Length.Should().Be(1); result[0].PropertyName.Should().Be("Grid.Column"); }
protected virtual ISingleBindingSourceAccessor GetAccessor(object model, string path, IDataContext context, bool isSource) { var observer = new MultiPathObserver(model, BindingPath.Create(path), false); var source = isSource ? new BindingSource(observer) : new BindingTarget(observer); return(new BindingSourceAccessor(source, context, !isSource)); }
public void IndexedPathStorageTest() { string rawPath = "AvailableEmployees.Name"; BindingPath path = new BindingPath(rawPath, 1); Assert.AreEqual(rawPath, path.Raw); Assert.AreNotEqual(path.Raw, path.FullyQualified); Assert.AreEqual(path.Indexed, path.FullyQualified); }
public static IBindingToSyntax Bind([NotNull] this IBindingBuilder builder, [NotNull] object target, [NotNull] string targetPath) { Should.NotBeNull(builder, "builder"); Should.NotBeNull(target, "target"); Should.NotBeNullOrWhitespace(targetPath, "targetPath"); builder.Add(BindingBuilderConstants.Target, target); builder.Add(BindingBuilderConstants.TargetPath, BindingPath.Create(targetPath)); return(builder.GetOrAddSyntaxBuilder()); }
protected virtual ISingleBindingSourceAccessor GetAccessor(object model, string path, IDataContext context, bool isSource, Func<IDataContext, object> commandParameterDelegate = null) { var source = new MultiPathObserver(model, BindingPath.Create(path), false); if (commandParameterDelegate != null) { context = context.ToNonReadOnly(); context.AddOrUpdate(BindingBuilderConstants.CommandParameter, commandParameterDelegate); } return new BindingSourceAccessor(source, context, !isSource); }
private static (object target, BindingPath binding) ArrangeIncorrect(DependencyPropertyValuePrecedences?precedence = null) { var target = new object(); var binding = new BindingPath(nameof(MyTarget.Value), MyTarget.FallbackValue, precedence, false) { DataContext = target }; return(target, binding); }
public void When_Parse_AttachedProperties() { var sut = new BindingPath("(hello.world).(bonjour:le.monde)", null); var result = sut.GetPathItems().ToArray(); result.Length.Should().Be(2); result[0].PropertyName.Should().Be("hello.world"); result[1].PropertyName.Should().Be("bonjour:le.monde"); }
public void When_Parse_TrimItemPath() { var sut = new BindingPath(" hello [world ]( bonjour:le.monde ).value ", null); var result = sut.GetPathItems().ToArray(); result.Length.Should().Be(4); result[0].PropertyName.Should().Be("hello"); result[1].PropertyName.Should().Be("[world ]"); result[2].PropertyName.Should().Be("bonjour:le.monde"); result[3].PropertyName.Should().Be("value"); }
public void SimplePathStorageTest() { string rawPath = "SelectedEmployee.Name"; BindingPath path = new BindingPath(rawPath); Assert.AreEqual(rawPath, path.Raw); Assert.AreEqual(path.Raw, path.FullyQualified); Assert.AreEqual(path.Raw, path.Indexed); //Default mode, non other specfied Assert.AreEqual(PathMode.Relative, path.Mode); }
public void When_Parse_SimpleProperties() { var sut = new BindingPath("hello.world.bonjour.le.monde", null); var result = sut.GetPathItems().ToArray(); result.Length.Should().Be(5); result[0].PropertyName.Should().Be("hello"); result[1].PropertyName.Should().Be("world"); result[2].PropertyName.Should().Be("bonjour"); result[3].PropertyName.Should().Be("le"); result[4].PropertyName.Should().Be("monde"); }
private static IBindingPath BindingPathFactoryImpl(string path) { lock (BindingPathCache) { IBindingPath value; if (!BindingPathCache.TryGetValue(path, out value)) { value = new BindingPath(path); BindingPathCache[path] = value; } return(value); } }
public void When_Parse_Indexers() { var sut = new BindingPath("[hello][world][bonjour][le][monde]", null); var result = sut.GetPathItems().ToArray(); result.Length.Should().Be(5); result[0].PropertyName.Should().Be("[hello]"); result[1].PropertyName.Should().Be("[world]"); result[2].PropertyName.Should().Be("[bonjour]"); result[3].PropertyName.Should().Be("[le]"); result[4].PropertyName.Should().Be("[monde]"); }
public virtual void BindingShouldSuppressCycle() { var cycleItem1 = new CycleItem(); var cycleItem2 = new CycleItem(); var dataBinding = CreateDataBinding( new BindingSourceAccessor(new SinglePathObserver(cycleItem1, BindingPath.Create("Property"), true), DataContext.Empty, true), new BindingSourceAccessor(new SinglePathObserver(cycleItem2, BindingPath.Create("Property"), true), DataContext.Empty, false)); dataBinding.Behaviors.Add(new TwoWayBindingMode()); cycleItem2.Property = 10; Tracer.Warn("Item1: {0}, Item2: {1}", cycleItem1.Property, cycleItem2.Property); }
public CollectionViewGroup(object group, PropertyPath itemsPath) { Group = group; if (itemsPath != null) { _bindingPath = new BindingPath(itemsPath.Path, null); _bindingPath.DataContext = group; GroupItems = ObservableVectorWrapper.Create(_bindingPath.Value); } else { GroupItems = ObservableVectorWrapper.Create(group); } }
public void SimplePathSourceResolutionTest() { string rawPath = "SelectedEmployee.Name"; BindingPath path = new BindingPath(rawPath); object selectedEmployee = new { Name = "Sam" }; SourceProperty sourceProperty = path.ResolveAsSource(new { SelectedEmployee = selectedEmployee }); Assert.IsNotNull(sourceProperty); Assert.IsNotNull(sourceProperty.Descriptor); Assert.IsNotNull(sourceProperty.OwningInstance); Assert.IsNotNull(sourceProperty.Value); Assert.AreEqual("Sam", sourceProperty.Value); Assert.AreEqual("Name", sourceProperty.Descriptor.Name); Assert.AreEqual(selectedEmployee, sourceProperty.OwningInstance); }