public void When_DependencyProperty_FallbackValue_binding() { MyControl.MyPropertyProperty.ToString(); var SUT = new MyControl(); SUT.SetBinding(MyControl.MyPropertyProperty, new Binding("TargetValue") { FallbackValue = 42 }); Assert.AreEqual(42, SUT.MyProperty); var source = new Target2(); source.TargetValue = 10; SUT.DataContext = source; Assert.AreEqual(10, SUT.MyProperty); SUT.DataContext = null; Assert.AreEqual(42, SUT.MyProperty); SUT.DataContext = source; Assert.AreEqual(10, SUT.MyProperty); }
public void When_DependencyProperty_FallbackValue_VisibilityStringConversion_binding() { MyControl.MyPropertyProperty.ToString(); var SUT = new MyControl(); var source = new Target2(); Assert.AreEqual(Visibility.Visible, SUT.MyVisibilityProperty); SUT.SetBinding(MyControl.MyVisibilityPropertyProperty, new Binding("Object") { FallbackValue = "Collapsed" }); Assert.AreEqual(Visibility.Collapsed, SUT.MyVisibilityProperty); SUT.DataContext = source; Assert.AreEqual(Visibility.Visible, SUT.MyVisibilityProperty); source.Object = 10; Assert.AreEqual(Visibility.Collapsed, SUT.MyVisibilityProperty); source.Object = Visibility.Visible; Assert.AreEqual(Visibility.Visible, SUT.MyVisibilityProperty); SUT.DataContext = null; Assert.AreEqual(Visibility.Collapsed, SUT.MyVisibilityProperty); }
public void When_DependencyProperty_TargetNullValue_binding() { var tomato = SolidColorBrushHelper.Tomato; MyControl.MyPropertyProperty.ToString(); var SUT = new MyControl(); SUT.SetBinding(MyControl.MyBrushPropertyProperty, new Binding("Brush") { TargetNullValue = tomato }); var source = new Target2(); SUT.DataContext = source; source.Brush = null; Assert.AreEqual(tomato, SUT.MyBrushProperty); source.Brush = SolidColorBrushHelper.Olive; Assert.AreEqual(SolidColorBrushHelper.Olive.Color, (SUT.MyBrushProperty as SolidColorBrush)?.Color); source.Brush = null; Assert.AreEqual(tomato, SUT.MyBrushProperty); }
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); }
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); }
public void When_DependencyProperty_OneWay_binding() { var SUT = new MyControl(); SUT.SetBinding(MyControl.MyPropertyProperty, new Binding("TargetValue")); var source = new Target2(); source.TargetValue = 42; SUT.DataContext = source; Assert.AreEqual(42, SUT.MyProperty); SUT.MyProperty = 43; Assert.AreEqual(42, source.TargetValue); }
public void When_DataContext_Changed_With_TwoWay_Binding() { // Tests both Value and Reference types var targetA = new Target2() { TargetValue = 10, Brush = SolidColorBrushHelper.Tomato }; var targetB = new Target2() { TargetValue = 20, Brush = SolidColorBrushHelper.Olive }; var control = new MyControl(); control.SetBinding("MyProperty", new Binding { Path = "TargetValue", Mode = BindingMode.TwoWay }); control.SetBinding("MyBrushProperty", new Binding { Path = "Brush", Mode = BindingMode.TwoWay }); control.DataContext = targetA; Assert.AreEqual(10, targetA.TargetValue); Assert.AreEqual(SolidColorBrushHelper.Tomato.Color, (targetA.Brush as SolidColorBrush).Color); // This test used to fail because changing the DataContext would dispose the previous PropertyChanged subscription, // which would clear the value and RaisePropertyChanged with null, // which would propagate that value up to the new DataContext (with TwoWay binding enabled). control.DataContext = targetB; Assert.AreEqual(20, targetB.TargetValue); Assert.AreEqual(SolidColorBrushHelper.Olive.Color, (targetB.Brush as SolidColorBrush).Color); // Making sure tat setting DataContext to null doesn't affect previous DataContexts control.DataContext = null; Assert.AreEqual(10, targetA.TargetValue); Assert.AreEqual(20, targetB.TargetValue); Assert.AreEqual(SolidColorBrushHelper.Tomato.Color, (targetA.Brush as SolidColorBrush).Color); Assert.AreEqual(SolidColorBrushHelper.Olive.Color, (targetB.Brush as SolidColorBrush).Color); // Making sure that TargetValue is only set once (object initializer) // There used to be issues where changing the DataContext would cause TwoWay binding to propagate invalid changes to the source. Assert.AreEqual(1, targetA.TargetValueSetCount); Assert.AreEqual(1, targetA.TargetValueSetCount); Assert.AreEqual(1, targetA.BrushSetCount); Assert.AreEqual(1, targetA.BrushSetCount); }
public void When_Object_AttachedProperty_binding() { var SUT = new Target1(); SUT.SetBinding(Attachable.MyValueProperty, new Binding("TargetValue")); var source = new Target2(); source.TargetValue = 42; SUT.DataContext = source; Assert.AreEqual(42, Attachable.GetMyValue(SUT)); Attachable.SetMyValue(SUT, 43); Assert.AreEqual(42, source.TargetValue); }
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); }
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); }
public void When_DependencyProperty_TWoWay_binding() { MyControl.MyPropertyProperty.ToString(); var SUT = new MyControl(); SUT.SetBinding(MyControl.MyPropertyProperty, new Binding("TargetValue") { Mode = BindingMode.TwoWay }); var source = new Target2(); source.TargetValue = 42; SUT.DataContext = source; Assert.AreEqual(42, SUT.MyProperty); SUT.MyProperty = 43; Assert.AreEqual(43, source.TargetValue); }
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); }