public void Should_Bind_To_Element() { TextBlock source; ContentControl target; var root = new TestRoot { Child = new StackPanel { Children = new Controls.Controls { (source = new TextBlock { Name = "source", Text = "foo", }), (target = new ContentControl { Name = "target", }) } } }; var binding = new Binding { ElementName = "source", }; binding.Bind(target, ContentControl.ContentProperty); Assert.Same(source, target.Content); }
public void Should_Bind_To_Element_Path() { TextBlock target; var root = new TestRoot { Child = new StackPanel { Children = new Controls.Controls { new TextBlock { Name = "source", Text = "foo", }, (target = new TextBlock { Name = "target", }) } } }; var binding = new Binding { ElementName = "source", Path = "Text", }; binding.Bind(target, TextBlock.TextProperty); Assert.Equal("foo", target.Text); }
public void OneTime_Binding_Should_Be_Set_Up() { var dataContext = new BehaviorSubject<object>(null); var expression = new BehaviorSubject<object>(null); var target = CreateTarget(dataContext: dataContext); var binding = new Binding { Path = "Foo", Mode = BindingMode.OneTime, }; binding.Bind(target.Object, TextBox.TextProperty, expression); target.Verify(x => x.SetValue( (PerspexProperty)TextBox.TextProperty, null, BindingPriority.LocalValue)); target.ResetCalls(); expression.OnNext("foo"); dataContext.OnNext(1); target.Verify(x => x.SetValue( (PerspexProperty)TextBox.TextProperty, "foo", BindingPriority.LocalValue)); }
public void Should_Bind_To_Later_Added_Element() { TextBlock target; StackPanel stackPanel; var root = new TestRoot { Child = stackPanel = new StackPanel { Children = new Controls.Controls { (target = new TextBlock { Name = "target", }), } } }; var binding = new Binding { ElementName = "source", Path = "Text", }; binding.Bind(target, TextBlock.TextProperty); stackPanel.Children.Add(new TextBlock { Name = "source", Text = "foo", }); Assert.Equal("foo", target.Text); }
public void Source_Should_Be_Used() { var source = new Source { Foo = "foo" }; var binding = new Binding { Source = source, Path = "Foo" }; var target = new TextBlock(); target.Bind(TextBlock.TextProperty, binding); Assert.Equal(target.Text, "foo"); }
public void OneWayToSource_Binding_Should_Be_Set_Up() { var textObservable = new Mock<IObservable<string>>(); var expression = new Mock<ISubject<object>>(); var target = CreateTarget(text: textObservable.Object); var binding = new Binding { Path = "Foo", Mode = BindingMode.OneWayToSource, }; binding.Bind(target.Object, TextBox.TextProperty, expression.Object); textObservable.Verify(x => x.Subscribe(expression.Object)); }
public void OneWay_Binding_Should_Be_Set_Up() { var target = CreateTarget(); var binding = new Binding { SourcePropertyPath = "Foo", Mode = BindingMode.OneWay, }; binding.Bind(target.Object, TextBox.TextProperty); target.Verify(x => x.Bind( TextBox.TextProperty, It.IsAny<IObservable<object>>(), BindingPriority.LocalValue)); }
public void TwoWay_Binding_Should_Be_Set_Up() { var target = CreateTarget(); var binding = new Binding { Path = "Foo", Mode = BindingMode.TwoWay, }; binding.Bind(target.Object, TextBox.TextProperty); target.Verify(x => x.BindTwoWay( TextBox.TextProperty, It.IsAny<ISubject<object>>(), BindingPriority.LocalValue)); }
public void OneWay_Binding_Should_Be_Set_Up() { var target = CreateTarget(); var binding = new Binding { Mode = BindingMode.OneWay, RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Priority = BindingPriority.TemplatedParent, Path = "Foo", }; target.Object.Bind(TextBox.TextProperty, binding); target.Verify(x => x.Bind( TextBox.TextProperty, It.IsAny<IObservable<object>>(), BindingPriority.TemplatedParent)); }
public void TwoWay_Binding_Should_Be_Set_Up() { var target = CreateTarget(); var binding = new Binding { Mode = BindingMode.TwoWay, RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent), Priority = BindingPriority.TemplatedParent, Path = "Foo", }; binding.Bind(target.Object, TextBox.TextProperty); target.Verify(x => x.BindTwoWay( TextBox.TextProperty, It.IsAny<ISubject<object>>(), BindingPriority.TemplatedParent)); }
public void OneWayToSource_Binding_Should_Be_Set_Up() { var source = new Source { Foo = "foo" }; var target = new TextBlock { DataContext = source, Text = "bar" }; var binding = new Binding { Path = "Foo", Mode = BindingMode.OneWayToSource, }; target.Bind(TextBox.TextProperty, binding); Assert.Equal("bar", source.Foo); target.Text = "baz"; Assert.Equal("baz", source.Foo); source.Foo = "quz"; Assert.Equal("baz", target.Text); }
public void Should_Not_Write_To_Old_DataContext() { var vm = new OldDataContextViewModel(); var target = new OldDataContextTest(); var fooBinding = new Binding { Path = "Foo", Mode = BindingMode.TwoWay, }; var barBinding = new Binding { Path = "Bar", Mode = BindingMode.TwoWay, }; // Bind Foo and Bar to the VM. fooBinding.Bind(target, OldDataContextTest.FooProperty); barBinding.Bind(target, OldDataContextTest.BarProperty); target.DataContext = vm; // Make sure the control's Foo and Bar properties are read from the VM Assert.Equal(1, target.GetValue(OldDataContextTest.FooProperty)); Assert.Equal(2, target.GetValue(OldDataContextTest.BarProperty)); // Set DataContext to null. target.DataContext = null; // Foo and Bar are no longer bound so they return 0, their default value. Assert.Equal(0, target.GetValue(OldDataContextTest.FooProperty)); Assert.Equal(0, target.GetValue(OldDataContextTest.BarProperty)); // The problem was here - DataContext is now null, setting Foo to 0. Bar is bound to // Foo so Bar also gets set to 0. However the Bar binding still had a reference to // the VM and so vm.Bar was set to 0 erroneously. Assert.Equal(1, vm.Foo); Assert.Equal(2, vm.Bar); }
public void Should_Use_Supplied_Converter() { var target = CreateTarget(null); var converter = new Mock<IValueConverter>(); var binding = new Binding { Converter = converter.Object, Path = "Foo", }; var result = binding.CreateSubject(target.Object, TextBox.TextProperty.PropertyType); Assert.Same(converter.Object, ((ExpressionSubject)result).Converter); }
public void Should_Use_DefaultValueConverter_When_No_Converter_Specified() { var target = CreateTarget(null); var binding = new Binding { Path = "Foo", }; var result = binding.CreateSubject(target.Object, TextBox.TextProperty.PropertyType); Assert.IsType<DefaultValueConverter>(((ExpressionSubject)result).Converter); }
public void DataContext_Binding_Should_Use_Parent_DataContext() { var parentDataContext = Mock.Of<IHeadered>(x => x.Header == (object)"Foo"); var parent = new Decorator { Child = new Control(), DataContext = parentDataContext, }; var binding = new Binding { Path = "Header", }; binding.Bind(parent.Child, Control.DataContextProperty); Assert.Equal("Foo", parent.Child.DataContext); parentDataContext = Mock.Of<IHeadered>(x => x.Header == (object)"Bar"); parent.DataContext = parentDataContext; Assert.Equal("Bar", parent.Child.DataContext); }
public void Should_Not_Write_To_Old_DataContext() { var vm = new OldDataContextViewModel(); var target = new TestSelector(); var itemsBinding = new Binding { Path = "Items", Mode = BindingMode.OneWay, }; var selectedItemsBinding = new Binding { Path = "SelectedItems", Mode = BindingMode.OneWay, }; // Bind Items and SelectedItems to the VM. itemsBinding.Bind(target, TestSelector.ItemsProperty); selectedItemsBinding.Bind(target, TestSelector.SelectedItemsProperty); // Set DataContext and SelectedIndex target.DataContext = vm; target.SelectedIndex = 1; // Make sure SelectedItems are written back to VM. Assert.Equal(new[] { "bar" }, vm.SelectedItems); // Clear DataContext and ensure that SelectedItems is still set in the VM. target.DataContext = null; Assert.Equal(new[] { "bar" }, vm.SelectedItems); // Ensure target's SelectedItems is now clear. Assert.Empty(target.SelectedItems); }
public void Default_BindingMode_Should_Be_Used() { var source = new Source { Foo = "foo" }; var target = new TwoWayBindingTest { DataContext = source }; var binding = new Binding { Path = "Foo", }; target.Bind(TwoWayBindingTest.TwoWayProperty, binding); Assert.Equal("foo", target.TwoWay); source.Foo = "bar"; Assert.Equal("bar", target.TwoWay); target.TwoWay = "baz"; Assert.Equal("baz", source.Foo); }
public void Should_Return_FallbackValue_When_Path_Not_Resolved() { var target = new TextBlock(); var source = new Source(); var binding = new Binding { Source = source, Path = "BadPath", FallbackValue = "foofallback", }; target.Bind(TextBlock.TextProperty, binding); Assert.Equal("foofallback", target.Text); }
public void Default_BindingMode_Should_Be_Used() { // Default for TextBox.Text is two-way. var source = new Source { Foo = "foo" }; var target = new TextBlock { DataContext = source }; var binding = new Binding { Path = "Foo", }; target.Bind(TextBox.TextProperty, binding); Assert.Equal("foo", target.Text); source.Foo = "bar"; Assert.Equal("bar", target.Text); target.Text = "baz"; Assert.Equal("baz", source.Foo); }
public void Should_Bind_To_Later_Added_Element() { ContentControl target; StackPanel stackPanel; var root = new TestRoot { Child = stackPanel = new StackPanel { Children = new Controls.Controls { (target = new ContentControl { Name = "target", }), } } }; var binding = new Binding { ElementName = "source", }; binding.Bind(target, ContentControl.ContentProperty); var source = new TextBlock { Name = "source", Text = "foo", }; stackPanel.Children.Add(source); Assert.Same(source, target.Content); }
public void Should_Return_FallbackValue_When_Invalid_Source_Type() { var target = new ProgressBar(); var source = new Source { Foo = "foo" }; var binding = new Binding { Source = source, Path = "Foo", FallbackValue = 42, }; target.Bind(ProgressBar.ValueProperty, binding); Assert.Equal(42, target.Value); }
public void Default_BindingMode_Should_Be_Used() { var target = CreateTarget(null); var binding = new Binding { Path = "Foo", }; binding.Bind(target.Object, TextBox.TextProperty); // Default for TextBox.Text is two-way. target.Verify(x => x.BindTwoWay( TextBox.TextProperty, It.IsAny<ISubject<object>>(), BindingPriority.LocalValue)); }
public void DataContext_Binding_Should_Track_Parent() { var parent = new Decorator { DataContext = new { Foo = "foo" }, }; var child = new Control(); var binding = new Binding { Path = "Foo", }; binding.Bind(child, Control.DataContextProperty); Assert.Null(child.DataContext); parent.Child = child; Assert.Equal("foo", child.DataContext); }
public void Unbound_SelectedItems_Should_Be_Cleared_When_DataContext_Cleared() { var data = new { Items = new[] { "foo", "bar", "baz" }, }; var target = new TestSelector { DataContext = data, Template = Template(), }; var itemsBinding = new Binding { Path = "Items" }; itemsBinding.Bind(target, TestSelector.ItemsProperty); Assert.Same(data.Items, target.Items); target.SelectedItems.Add("bar"); target.DataContext = null; Assert.Empty(target.SelectedItems); }
public void Should_Pass_ConverterParameter_To_Supplied_Converter() { var target = CreateTarget(); var converter = new Mock<IValueConverter>(); var binding = new Binding { Converter = converter.Object, ConverterParameter = "foo", Path = "Bar", }; var result = binding.CreateSubject(target.Object, TextBox.TextProperty.PropertyType); Assert.Same("foo", ((ExpressionSubject)result).ConverterParameter); }