예제 #1
0
        public void When_Inherited_data_Context_and_Changed()
        {
            var SUT = new Target1();

            var child = new Target2(SUT);

            child.SetBinding(Target2.DataContextProperty, new Binding("Child"));
            child.SetBinding("TargetValue", new Binding("Value"));
            SUT.ChildrenBinders.Add(child);

            var source = new MySource();

            SUT.DataContext = source;

            Assert.AreEqual(42, child.TargetValue);
            Assert.AreEqual(1, child.TargetValueSetCount);
            Assert.AreEqual(1, SUT.DataContextChangedCount);
            Assert.AreEqual(1, child.DataContextChangedCount);

            source.Child = new MySource2(44);
            Assert.AreEqual(44, child.TargetValue);
            Assert.AreEqual(2, child.TargetValueSetCount);
            Assert.AreEqual(1, SUT.DataContextChangedCount);
            Assert.AreEqual(2, child.DataContextChangedCount);
        }
예제 #2
0
        public void When_Inherited_data_Context_And_Converter_Different_Types_Invalid_Child_Path()
        {
            var SUT = new Target1();

            var child = new Target2(SUT);

            var converter = new BoolToNumber();

            child.SetBinding("DataContext", new Binding("Item.List[0]"));
            child.SetBinding("TargetValue", new Binding("Details_zzz.InfoBoolean", converter: converter)
            {
                FallbackValue = 10
            });
            SUT.ChildrenBinders.Add(child);

            // With the invalid path (Details_zzz instead of Details), the converter should not be called at all.
            // FallbackValue should be used
            SUT.DataContext = new SourceLevel0()
            {
                Item = new SourceLevel1()
                {
                    List = new SourceLevel2[] { new SourceLevel2() }
                }
            };
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(0, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);
        }
예제 #3
0
        public void When_Direct_data_Context_binding()
        {
            var SUT = new Target1();

            var child = new Target2();

            // Bypass the inheritance
            child.SetBinding(Target1.DataContextProperty, new Binding("Child"));

            child.SetBinding(Target2.TargetValueProperty, new Binding("Value"));

            SUT.ChildrenBinders.Add(child);

            var source = new MySource();

            SUT.DataContext = source;

            Assert.AreEqual(42, child.TargetValue);
            Assert.AreEqual(1, child.TargetValueSetCount);
        }
예제 #4
0
        public void When_Inherited_data_Context_And_Converter()
        {
            var SUT = new Target1();

            var child = new Target2(SUT);

            child.SetBinding(Target2.DataContextProperty, new Binding("Child"));
            child.SetBinding("TargetValue", new Binding("Value", converter: new OppositeConverter()));
            SUT.ChildrenBinders.Add(child);

            var source = new MySource();

            SUT.DataContext = source;

            Assert.AreEqual(-42, child.TargetValue);
            Assert.AreEqual(1, child.TargetValueSetCount);

            source.Child = new MySource2(43);
            Assert.AreEqual(-43, child.TargetValue);
            Assert.AreEqual(2, child.TargetValueSetCount);
        }
예제 #5
0
        public void When_Inherited_data_Context_Sequence_And_Converter()
        {
            var SUT = new Target1();

            var child = new Target2(SUT);

            var converter = new OppositeConverter();

            child.SetBinding(Target2.DataContextProperty, new Binding("Item.List[0]"));
            child.SetBinding("TargetValue", new Binding("Details.Info", converter: converter)
            {
                FallbackValue = 10
            });
            SUT.ChildrenBinders.Add(child);

            SUT.DataContext = new SourceLevel0();
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(0, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);

            SUT.DataContext = null;
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(0, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);

            SUT.DataContext = new SourceLevel0();
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(0, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);

            SUT.DataContext = null;
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(0, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);

            SUT.DataContext = new SourceLevel0()
            {
                Item = new SourceLevel1()
                {
                    List = new SourceLevel2[] { new SourceLevel2() }
                }
            };
            Assert.AreEqual(-1000, child.TargetValue);
            Assert.AreEqual(1, converter.ConversionCount);
            Assert.AreEqual(1000, converter.LastValue);

            // It breaks here, when a broken binding would replace a fully functional one.
            SUT.DataContext = new SourceLevel0();
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(2, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);

            SUT.DataContext = new SourceLevel0()
            {
                Item = new SourceLevel1()
                {
                    List = new SourceLevel2[] { new SourceLevel2() }
                }
            };
            Assert.AreEqual(-1000, child.TargetValue);
            Assert.AreEqual(3, converter.ConversionCount);
            Assert.AreEqual(1000, converter.LastValue);

            SUT.DataContext = null;
            Assert.AreEqual(10, child.TargetValue);
            Assert.AreEqual(4, converter.ConversionCount);
            Assert.AreEqual(null, converter.LastValue);

            SUT.DataContext = new SourceLevel0()
            {
                Item = new SourceLevel1()
                {
                    List = new SourceLevel2[] { new SourceLevel2() }
                }
            };
            Assert.AreEqual(-1000, child.TargetValue);
            Assert.AreEqual(5, converter.ConversionCount);
            Assert.AreEqual(1000, converter.LastValue);
        }