public void BindingTest() { var c = new { X = 1, Y = new { Z = new[] { 1, 2, 3 } }, T = new Dictionary<string, int> {["Four"] = 4 } }; var root = new HierarchicalObjectImp { DataContext = c }; var child = new HierarchicalObjectImp {HierarchyParent = root}; var descendant = new HierarchicalObjectImp {HierarchyParent = child}; root.SetBinding(HierarchicalObjectImp.BarProperty, "."); Assert.AreEqual(root.Bar, c); root.SetBinding(HierarchicalObjectImp.FooProperty, "X"); Assert.AreEqual(root.Foo, 1); root.SetBinding(HierarchicalObjectImp.FooProperty, "Y.Z[2]"); Assert.AreEqual(root.Foo, 3); root.SetBinding(HierarchicalObjectImp.FooProperty, "T[\"Four\"]"); Assert.AreEqual(root.Foo, 4); child.SetBinding(HierarchicalObjectImp.FooProperty, "Y.Z[1]"); Assert.AreEqual(child.Foo, 2); // test inheritance root.DataContext = new {Foo = 11, Bar = 22, Baz = 33}; child.SetBinding(HierarchicalObjectImp.FooProperty, "Bar"); Assert.AreEqual(child.Foo, 22); descendant.SetBinding(HierarchicalObjectImp.FooProperty, "Foo"); Assert.AreEqual(descendant.Foo, 11); // Test own data context descendant.DataContext = new {B = 7}; root.SetBinding(HierarchicalObjectImp.FooProperty, "Baz"); Assert.AreEqual(root.Foo, 33); descendant.SetBinding(HierarchicalObjectImp.FooProperty, "B"); Assert.AreEqual(descendant.Foo, 7); }
public void BindingDataContextTest() { var c = new {Y = new { Z = new[] { 1, 2, 3 } }}; var root = new HierarchicalObjectImp { DataContext = c }; var child = new HierarchicalObjectImp { HierarchyParent = root }; var descendant = new HierarchicalObjectImp { HierarchyParent = child }; child.SetBinding(HierarchicalObject.DataContextProperty, "Y"); Assert.AreEqual(child.DataContext, c.Y); descendant.SetBinding(HierarchicalObject.DataContextProperty, "Z[2]"); Assert.AreEqual(descendant.DataContext, 3); c = new { Y = new { Z = new[] { 11, 22, 33 } } }; root.DataContext = c; Assert.AreEqual(descendant.DataContext, 33); }
public void BindingErrorTest() { var c = new { X = 1, Y = "foo", T = new Dictionary<string, int> {["Four"] = 4 } }; var root = new HierarchicalObjectImp { DataContext = c }; object trash; Assert.Throws<ArgumentNullException>(() => root.SetBinding(HierarchicalObjectImp.BarProperty, null)); Assert.Throws<ArgumentException>(() => root.SetBinding(HierarchicalObjectImp.BarProperty, "")); root.SetBinding(HierarchicalObjectImp.FooProperty, "X'"); Assert.Throws<BindingExceprtion>(() => trash = root.Foo); root.SetBinding(HierarchicalObjectImp.FooProperty, "Y.Z['2]"); Assert.Throws<BindingExceprtion>(() => trash = root.Foo); root.SetBinding(HierarchicalObjectImp.FooProperty, "T[Four\"]"); Assert.Throws<BindingExceprtion>(() => trash = root.Foo); root.SetBinding(HierarchicalObjectImp.FooProperty, "Y"); Assert.Throws<BindingTypeMismatchExceprtion>(() => trash = root.Foo); }
public void LooseBindingTest() { var c = new { Y = new { Z = new[] { 1, 2, 3 } } }; var root = new HierarchicalObjectImp { DataContext = c }; // This will also ignore syntax errors root.SetBinding(HierarchicalObjectImp.FooProperty, "X'", true); Assert.AreEqual(root.Foo, 0); root.SetBinding(HierarchicalObjectImp.FooProperty, "X", true); Assert.AreEqual(root.Foo, 0); root.SetBinding(HierarchicalObjectImp.BarProperty, "T", true); Assert.AreEqual(root.Bar, null); }