public void DataContext_Binding_Should_Produce_Correct_Results() { var root = new Decorator { DataContext = new { Foo = "bar" }, }; var child = new Control(); var dataContextBinding = new Binding("Foo"); var values = new List<object>(); child.GetObservable(Border.DataContextProperty).Subscribe(x => values.Add(x)); child.Bind(ContentControl.DataContextProperty, dataContextBinding); root.Child = child; Assert.Equal(new[] { null, "bar" }, values); }
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", }; child.Bind(Control.DataContextProperty, binding); Assert.Null(child.DataContext); parent.Child = child; Assert.Equal("foo", child.DataContext); }
public void DataContext_Binding_Should_Produce_Correct_Results() { var viewModel = new { Foo = "bar" }; var root = new Decorator { DataContext = viewModel, }; var child = new Control(); var values = new List<object>(); child.GetObservable(Control.DataContextProperty).Subscribe(x => values.Add(x)); child.Bind(Control.DataContextProperty, new Binding("Foo")); // When binding to DataContext and the target isn't found, the binding should produce // null rather than UnsetValue in order to not propagate incorrect DataContexts from // parent controls while things are being set up. This logic is implemented in // `Avalonia.Markup.Xaml.Binding.Initiate`. Assert.True(child.IsSet(Control.DataContextProperty)); root.Child = child; Assert.Equal(new[] { null, "bar" }, values); }