コード例 #1
0
        public void Test_BindSource_IndexPropertyInt()
        {
            var viewModel = new TestViewModel();
            var bindSource = new BindContext(viewModel, "TestViewModel2.TestViewModel3.IntValues[1]");

            var viewModel2 = new TestViewModel2();
            viewModel.TestViewModel2 = viewModel2;

            var viewModel3 = new TestViewModel3();
            viewModel2.TestViewModel3 = viewModel3;

            Assert.AreEqual(null, bindSource.PropertyType);
            Assert.AreEqual(null, bindSource.Value);

            var list = new ObservableCollection<int>();
            viewModel3.IntValues = list;
            Assert.AreEqual(list, bindSource.Source);
            Assert.AreEqual(typeof(int), bindSource.PropertyType);
            Assert.AreEqual(default(int), bindSource.Value);

            list.Add(1);
            list.Add(2);

            Assert.AreEqual(2, bindSource.Value);

            bindSource.Value = 2;
            Assert.AreEqual(2, list[1]);

            list.RemoveAt(1);

            Assert.AreEqual(default(int), bindSource.Value);
        }
コード例 #2
0
        public void Test_BindingEngine_ClearBinding_Object()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel2();

            BindingEngine.SetPropertyBinding(viewModel2, "Name", viewModel, "Name")
                .SetMode(BindMode.TwoWay);

            viewModel.Name = Name1;
            Assert.AreEqual(viewModel2.Name, Name1);

            BindingEngine.SetPropertyBinding(viewModel2, "Age", viewModel, "Age")
                .SetMode(BindMode.TwoWay);

            BindingEngine.ClearBinding(new TestViewModel());

            viewModel.Age = 2;
            Assert.AreEqual(viewModel2.Age, 2);

            BindingEngine.ClearBinding(viewModel2);

            viewModel.Name = Name2;
            Assert.AreNotEqual(viewModel2.Name, Name2);
            viewModel.Age = 3;
            Assert.AreNotEqual(viewModel2.Age, 3);
        }
コード例 #3
0
        public void Test_BindSource_IndexPropertyDictionary()
        {
            var viewModel = new TestViewModel();
            var bindSource = new BindContext(viewModel, "TestViewModel2.StringValues[yohan]");

            Assert.IsNull(bindSource.Source);
            Assert.IsNull(bindSource.Value);

            var viewModel2 = new TestViewModel2();
            viewModel.TestViewModel2 = viewModel2;

            Assert.IsNull(bindSource.Source);
            Assert.IsNull(bindSource.Value);

            var dictionary = new Dictionary<string, string>();
            viewModel.TestViewModel2.StringValues = dictionary;

            Assert.AreEqual(dictionary, bindSource.Source);
            Assert.IsNull(bindSource.Value);

            viewModel.TestViewModel2.StringValues = new Dictionary<string, string>();

            viewModel.TestViewModel2.StringValues.Add("yohan", "1");
            Assert.AreEqual("1", bindSource.Value);
        }
コード例 #4
0
        public void Test_MemoryLeak_WeakBinding_AttachEvent()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel2();
            var reference = new WeakReference(viewModel2);
            var reference2 = new WeakReference(viewModel);

            WeakBinding binding = new WeakPropertyBinding(viewModel2, "Name", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.TwoWay)
                .AttachTargetEvent("TestViewModelEvent");
            viewModel.Name = Name1;

            Assert.AreEqual(viewModel2.Name, Name1);

            viewModel2.Name = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            viewModel2.RaiseTestViewModelEvent();
            Assert.AreEqual(viewModel.Name, Name2);

            viewModel2 = null;
            GC.Collect();
            Assert.IsFalse(reference.IsAlive);

            viewModel = null;
            GC.Collect();
            Assert.IsFalse(reference2.IsAlive);

            GC.KeepAlive(binding);
        }
コード例 #5
0
        public void Test_MemoryLeak_BindSource()
        {
            var v = new TestViewModel();
            var v2 = new TestViewModel2();
            var reference = new WeakReference(v);
            var reference2 = new WeakReference(v2);

            int changeCount = 0;
            var bindSource = new BindContext(v, "TestViewModel2.Name");
            bindSource.SourceMode = SourceMode.Property;
            bindSource.SourceChanged += (sender, args) =>
                {
                    changeCount++;
                    Assert.AreNotEqual(args.OldSource, args.NewSource);
                    Assert.AreEqual(SourceMode.Property, args.SourceMode);
                };

            v.TestViewModel2 = v2;
            Assert.AreEqual(0, changeCount);

            v2 = null;
            v.TestViewModel2 = null;
            GC.Collect();
            Assert.IsFalse(reference2.IsAlive);

            v = null;
            GC.Collect();
            Assert.IsFalse(reference.IsAlive);

            GC.KeepAlive(bindSource);
        }
コード例 #6
0
        public void Test_BindingEngine_ClearBinding_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestViewModel2();

            BindingEngine.SetPropertyBinding(view, "Name", viewModel, "Name")
                .SetMode(BindMode.TwoWay);

            viewModel.Name = Name1;
            Assert.AreEqual(view.Name, Name1);

            BindingEngine.ClearBinding(view, i => i.Name, viewModel, o => o.Name);

            viewModel.Name = Name2;
            Assert.AreNotEqual(view.Name, Name2);
        }
コード例 #7
0
        public void Test_WeakBinding_DoConventions()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel2();

            var binding = new WeakPropertyBinding(viewModel2, "Name", viewModel, "Name")
                .Initialize<WeakPropertyBinding>();
            binding.DeActivate();

            Assert.IsFalse(binding.IsActivate);
            binding.Activate();

            viewModel.Name = Name1;

            binding.DeActivate();
            viewModel.Name = Name2;

            Assert.AreNotEqual(viewModel2.Name, Name2);

            binding.Activate();
            Assert.AreEqual(viewModel2.Name, Name2);
        }
コード例 #8
0
        public void Test_WeakMethodBinding_SourceMethod_2()
        {
            var viewModel2 = new TestViewModel2();
            var viewModel = new TestViewModel();
            var button = new Button();

            var binding = new WeakMethodBinding(viewModel2, null, button, null)
                .Initialize<WeakMethodBinding>()
                .AttachTargetMethod(new BindContext(viewModel, null), "AddAge", null)
                .AttachTargetCanInvokeMethod(new BindContext(viewModel, null), "CanAddAge", null)
                .AttachSourceEvent("Click")
                .OfType<WeakMethodBinding>();

            Assert.AreEqual(0, viewModel.Age);

            button.PerformClick();

            Assert.AreEqual(1, viewModel.Age);

            button.PerformClick();
            Assert.AreEqual(2, viewModel.Age);

            button.PerformClick();
            Assert.AreEqual(2, viewModel.Age);

            binding.DetachTargetCanInvokeMethod();
            button.PerformClick();
            Assert.AreEqual(3, viewModel.Age);

            binding.DetachTargetMethod();
            button.PerformClick();
            Assert.AreEqual(3, viewModel.Age);
        }
コード例 #9
0
        public void Test_BindingEngine_CollectionBinding_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            BindingEngine.SetCollectionBinding(view, o => o.TestViewModelCollections, viewModel,
                i => i.TestViewModelCollection)
                .Initialize<WeakCollectionBinding>();

            view.TestViewModelCollections = new List<TestViewModel2>();
            var collections = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = collections;

            var viewModel2 = new TestViewModel2();
            collections.Add(viewModel2);
            collections.Add(new TestViewModel2());

            Assert.AreEqual(2, view.TestViewModelCollections.Count);
            Assert.AreEqual(viewModel2, view.TestViewModelCollections[0]);

            collections.RemoveAt(0);
            Assert.AreEqual(1, view.TestViewModelCollections.Count);

            collections.Clear();
            Assert.AreEqual(0, view.TestViewModelCollections.Count);
        }
コード例 #10
0
        public void Test_BindingEngine_ClearBinding_Object_Property_Expression()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel2();
            var viewModel22 = new TestViewModel2();

            BindingEngine.SetPropertyBinding(viewModel, "Age", viewModel2, "Age");
            BindingEngine.SetPropertyBinding(viewModel, "Age", viewModel22, "Age");

            viewModel2.Age = 2;
            Assert.AreEqual(viewModel.Age, 2);
            viewModel22.Age = 3;
            Assert.AreEqual(viewModel.Age, 3);

            BindingEngine.ClearBinding(viewModel, model => model.Age);

            viewModel2.Age = 4;
            Assert.AreNotEqual(viewModel.Age, 4);
            viewModel22.Age = 5;
            Assert.AreNotEqual(viewModel.Age, 5);

            BindingEngine.SetPropertyBinding(viewModel, "Age", viewModel2, "Age");
            Assert.AreEqual(viewModel.Age, 4);
            BindingEngine.SetPropertyBinding(viewModel, "Age", viewModel22, "Age");
            Assert.AreEqual(viewModel.Age, 5);
        }
コード例 #11
0
        public void Test_WeakCollectionBinding_TwoWay()
        {
            var viewModel = new TestViewModel();
            var viewModel3 = new TestViewModel3();

            new WeakCollectionBinding(viewModel3, "TestViewModelCollection", viewModel, "TestViewModelCollection")
                .Initialize<WeakCollectionBinding>()
                .SetMode(BindMode.TwoWay);

            viewModel3.TestViewModelCollection = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = new ObservableCollection<TestViewModel2>();

            var viewModel2 = new TestViewModel2();
            viewModel3.TestViewModelCollection.Add(viewModel2);

            Assert.AreEqual(1, viewModel.TestViewModelCollection.Count);
            Assert.AreEqual(viewModel2, viewModel.TestViewModelCollection[0]);

            viewModel2 = new TestViewModel2();
            viewModel.TestViewModelCollection.Add(viewModel2);

            Assert.AreEqual(2, viewModel3.TestViewModelCollection.Count);
            Assert.AreEqual(viewModel2, viewModel3.TestViewModelCollection[1]);

            viewModel.TestViewModelCollection.Clear();
            Assert.AreEqual(0, viewModel3.TestViewModelCollection.Count);

            viewModel2 = new TestViewModel2();
            viewModel3.TestViewModelCollection.Add(viewModel2);
            Assert.AreEqual(1, viewModel.TestViewModelCollection.Count);

            viewModel3.TestViewModelCollection.RemoveAt(0);
            Assert.AreEqual(0, viewModel.TestViewModelCollection.Count);
        }
コード例 #12
0
        public void Test_WeakCollectionBinding_OneWayToTarget()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakCollectionBinding(viewModel, "TestViewModelCollection", view, "TestViewModelCollections")
                .Initialize<WeakCollectionBinding>()
                .SetMode(BindMode.OneWayToSource);

            view.TestViewModelCollections = new List<TestViewModel2>();
            var collections = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = collections;

            var viewModel2 = new TestViewModel2();
            collections.Add(viewModel2);
            collections.Add(new TestViewModel2());

            Assert.AreEqual(2, view.TestViewModelCollections.Count);
            Assert.AreEqual(viewModel2, view.TestViewModelCollections[0]);

            collections.RemoveAt(0);
            Assert.AreEqual(1, view.TestViewModelCollections.Count);

            collections.Clear();
            Assert.AreEqual(0, view.TestViewModelCollections.Count);
        }
コード例 #13
0
        public void Test_BindSource_IndexPropertyInt2()
        {
            var viewModel = new TestViewModel();
            var bindSource = new BindContext(viewModel, "TestViewModel2.TestViewModel3.TestViewModels[1].Name");

            var viewModel2 = new TestViewModel2();
            viewModel.TestViewModel2 = viewModel2;

            var viewModel3 = new TestViewModel3();
            viewModel2.TestViewModel3 = viewModel3;

            Assert.AreEqual(null, bindSource.Value);
            Assert.AreEqual(null, bindSource.Source);

            var testViewModels = new ObservableCollection<TestViewModel4>();
            viewModel3.TestViewModels = testViewModels;

            Assert.AreEqual(null, bindSource.Source);
            Assert.AreEqual(null, bindSource.Value);

            var t4 = new TestViewModel4();
            var t41 = new TestViewModel4();
            testViewModels.Add(t4);
            testViewModels.Add(t41);

            Assert.AreEqual(t41, bindSource.Source);
            Assert.AreEqual(null, bindSource.Value);

            t41.Name = Name;
            Assert.AreEqual(Name, bindSource.Value);

            testViewModels.RemoveAt(1);

            Assert.AreEqual(null, bindSource.Source);
            Assert.AreEqual(null, bindSource.Value);
        }
コード例 #14
0
        public void Test_BindSource_RecurisonProperty2()
        {
            var viewModel = new TestViewModel();
            var bindSource = new BindContext(viewModel, "TestViewModel2.TestViewModel3.Name");

            Assert.AreEqual(null, bindSource.Source);
            Assert.AreEqual(null, bindSource.Value);
            Assert.AreEqual(null, bindSource.PropertyType);

            var viewModel2 = new TestViewModel2();
            viewModel.TestViewModel2 = viewModel2;

            Assert.AreEqual(null, bindSource.Source);
            Assert.AreEqual(null, bindSource.Value);
            Assert.AreEqual(null, bindSource.PropertyType);

            var viewModel3 = new TestViewModel3();
            viewModel2.TestViewModel3 = viewModel3;

            Assert.AreEqual(viewModel3, bindSource.Source);
            Assert.AreEqual(typeof(string), bindSource.PropertyType);

            viewModel3.Name = Name;
            Assert.AreEqual(Name, bindSource.Value);
        }
コード例 #15
0
        public void Test_MemoryLeak_WeakPropertyBinding_Getter_Setter()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel2();

            var viewModel1 = new TestViewModel();
            var viewModel21 = new TestViewModel2();

            var reference = new WeakReference(viewModel1);
            var reference2 = new WeakReference(viewModel21);

            WeakBinding binding = new WeakPropertyBinding(viewModel2, null, viewModel, null)
                .Initialize<WeakPropertyBinding>()
                .SetMode<WeakPropertyBinding>(BindMode.TwoWay)
                .SetTargetPropertyGetter(arg => viewModel1.Name)
                .SetSourcePropertyGetter(arg => viewModel21.Name)
                .SetTargetPropertySetter((data, value) => viewModel1.Name = value.ToStringWithoutException())
                .SetSourcePropertySetter((data, value) => viewModel21.Name = value.ToStringWithoutException())
                .AttachTargetEvent("TestViewModelEvent")
                .AttachSourceEvent("TestViewModelEvent");

            viewModel21.Name = Name1;
            viewModel.RaiseTestViewModelEvent();
            Assert.AreEqual(viewModel1.Name, Name1);

            viewModel1.Name = Name2;
            viewModel2.RaiseTestViewModelEvent();
            Assert.AreEqual(viewModel21.Name, Name2);

            viewModel1 = null;
            GC.Collect();
            Assert.IsFalse(reference.IsAlive);

            viewModel21 = null;
            GC.Collect();
            Assert.IsFalse(reference2.IsAlive);

            GC.KeepAlive(binding);
        }