示例#1
0
        public void OneWayBindingTest()
        {
            ObservableNode sourceRoot = new ObservableNode();
            ObservableNode source     = new ObservableNode {
                Value = 1
            };

            DependencyObjectNode target = new DependencyObjectNode();

            int sourcePropertyChangedCount = 0;
            int targetPropertyChangedCount = 0;

            source.PropertyChanged += (sender, e) => sourcePropertyChangedCount++;
            target.PropertyChanged += (sender, e) => targetPropertyChangedCount++;

            ConversionCounter conversionCounter = new ConversionCounter();
            Binding           binding           = new Binding {
                Source = sourceRoot, Path = PropertyPath.Parse("Child.Value"), Mode = BindingMode.OneWay, Converter = conversionCounter
            };

            target.SetValue(DependencyObjectNode.ValueProperty, binding);

            Assert.AreEqual(0, target.Value_ClrWrapper);
            Assert.AreEqual(0, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            sourceRoot.Child = source;

            Assert.AreEqual(1, target.Value_ClrWrapper);
            Assert.AreEqual(1, targetPropertyChangedCount);
            Assert.AreEqual(1, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            source.Value = 2;

            Assert.AreEqual(2, target.Value_ClrWrapper);
            Assert.AreEqual(1, sourcePropertyChangedCount);
            Assert.AreEqual(2, targetPropertyChangedCount);
            Assert.AreEqual(2, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            target.Value_ClrWrapper = 3;

            Assert.AreEqual(2, source.Value);
            Assert.AreEqual(1, sourcePropertyChangedCount);
            Assert.AreEqual(3, targetPropertyChangedCount);
            Assert.AreEqual(2, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            sourceRoot.Child = null;

            Assert.AreEqual(0, target.Value_ClrWrapper);
            Assert.AreEqual(4, targetPropertyChangedCount);
            Assert.AreEqual(2, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);
        }
        public void OneWayBindingTest()
        {
            ObservableNode sourceRoot = new ObservableNode();
            ObservableNode source = new ObservableNode { Value = 1 };

            DependencyObjectNode target = new DependencyObjectNode();

            int sourcePropertyChangedCount = 0;
            int targetPropertyChangedCount = 0;

            source.PropertyChanged += (sender, e) => sourcePropertyChangedCount++;
            target.PropertyChanged += (sender, e) => targetPropertyChangedCount++;

            ConversionCounter conversionCounter = new ConversionCounter();
            Binding binding = new Binding { Source = sourceRoot, Path = PropertyPath.Parse("Child.Value"), Mode = BindingMode.OneWay, Converter = conversionCounter };

            target.SetValue(DependencyObjectNode.ValueProperty, binding);

            Assert.AreEqual(0, target.Value_ClrWrapper);
            Assert.AreEqual(0, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            sourceRoot.Child = source;

            Assert.AreEqual(1, target.Value_ClrWrapper);
            Assert.AreEqual(1, targetPropertyChangedCount);
            Assert.AreEqual(1, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            source.Value = 2;

            Assert.AreEqual(2, target.Value_ClrWrapper);
            Assert.AreEqual(1, sourcePropertyChangedCount);
            Assert.AreEqual(2, targetPropertyChangedCount);
            Assert.AreEqual(2, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            target.Value_ClrWrapper = 3;

            Assert.AreEqual(2, source.Value);
            Assert.AreEqual(1, sourcePropertyChangedCount);
            Assert.AreEqual(3, targetPropertyChangedCount);
            Assert.AreEqual(2, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);

            sourceRoot.Child = null;

            Assert.AreEqual(0, target.Value_ClrWrapper);
            Assert.AreEqual(4, targetPropertyChangedCount);
            Assert.AreEqual(2, conversionCounter.ConvertedCount);
            Assert.AreEqual(0, conversionCounter.ConvertedBackCount);
        }
        public void BindingFallbackValueTest()
        {
            ObservableNode sourceRoot = new ObservableNode();
            ObservableNode source = new ObservableNode();

            DependencyObjectNode target = new DependencyObjectNode();

            Binding binding = new Binding { Source = sourceRoot, Path = PropertyPath.Parse("Child.Value2"), Mode = BindingMode.TwoWay, FallbackValue = "fallback", TargetNullValue = "null" };

            target.SetValue(DependencyObjectNode.Value2Property, binding);

            Assert.AreEqual("fallback", target.Value2_ClrWrapper);

            sourceRoot.Child = source;

            Assert.AreEqual("null", target.Value2_ClrWrapper);

            source.Value2 = "value";

            Assert.AreEqual("value", target.Value2_ClrWrapper);
        }
示例#4
0
        public void BindingFallbackValueTest()
        {
            ObservableNode sourceRoot = new ObservableNode();
            ObservableNode source     = new ObservableNode();

            DependencyObjectNode target = new DependencyObjectNode();

            Binding binding = new Binding {
                Source = sourceRoot, Path = PropertyPath.Parse("Child.Value2"), Mode = BindingMode.TwoWay, FallbackValue = "fallback", TargetNullValue = "null"
            };

            target.SetValue(DependencyObjectNode.Value2Property, binding);

            Assert.AreEqual("fallback", target.Value2_ClrWrapper);

            sourceRoot.Child = source;

            Assert.AreEqual("null", target.Value2_ClrWrapper);

            source.Value2 = "value";

            Assert.AreEqual("value", target.Value2_ClrWrapper);
        }