Пример #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);
        }
Пример #2
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);
        }