Esempio n. 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);
        }
Esempio n. 2
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);
        }
Esempio n. 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);
        }
Esempio n. 4
0
 /// <summary>
 ///     Attaches the event to bind source.
 /// </summary>
 /// <param name="data">
 ///     The data.
 /// </param>
 /// <param name="eventName">
 ///     The event name.
 /// </param>
 /// <param name="filter">
 ///     The filter.
 /// </param>
 /// <returns>
 ///     The <see cref="WeakBinding"/>.
 /// </returns>
 internal WeakBinding AttachSourceEvent(BindContext data, string eventName, WeakEventFilterHandler filter = null)
 {
     this.SourceEvent.AttachEvent(data, eventName, SourceEventHandlerName, filter);
     return(this);
 }
Esempio n. 5
0
        /// <summary>
        ///     Attach event to bind source.
        /// </summary>
        /// <param name="target">
        ///     The target.
        /// </param>
        /// <param name="eventName">
        ///     The event name.
        /// </param>
        /// <param name="filter">
        ///     The filter.
        /// </param>
        /// <returns>
        ///     The <see cref="WeakBinding"/>.
        /// </returns>
        public WeakBinding AttachSourceEvent(object target, string eventName, WeakEventFilterHandler filter = null)
        {
            BindContext bindContext = this.GetBindContext(target, null);

            return(this.AttachSourceEvent(bindContext, eventName, filter));
        }
Esempio n. 6
0
            /// <summary>
            ///     Initializes a new instance of the <see cref="NotifyPropertyObservation"/> class.
            /// </summary>
            /// <param name="data">
            ///     The data.
            /// </param>
            /// <param name="property">
            ///     The property.
            /// </param>
            /// <param name="childProperty">
            ///     The child property.
            /// </param>
            public NotifyPropertyObservation(BindContext data, string property, string childProperty)
            {
                this.data = data;
                if (!string.IsNullOrEmpty(childProperty))
                {
                    this.child = data.CreatePropertyObservation(childProperty);
                }

                this.Property = property;
            }
Esempio n. 7
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="NotifyIndexPropertyObservation"/> class.
 /// </summary>
 /// <param name="data">
 ///     The data.
 /// </param>
 /// <param name="property">
 ///     The property.
 /// </param>
 /// <param name="childProperty">
 ///     The child property.
 /// </param>
 public NotifyIndexPropertyObservation(BindContext data, string property, string childProperty)
     : base(data, property, childProperty)
 {
 }
Esempio n. 8
0
        /// <summary>
        ///     Attaches the specific event.
        /// </summary>
        /// <param name="type">
        ///     The type.
        /// </param>
        /// <param name="eventName">
        ///     The event name.
        /// </param>
        /// <param name="handlerName">
        ///     The handler name.
        /// </param>
        /// <param name="filter">
        ///     The filter.
        /// </param>
        public void AttachEvent(Type type, string eventName, string handlerName, WeakEventFilterHandler filter = null)
        {
            this.DetachEvent();

            this.EventName = eventName;
            this.Data = null;
            this.targetType = type;

            this.eventAction = this.AttachEvent(eventName, handlerName, filter);
        }
Esempio n. 9
0
        /// <summary>
        ///     Attaches the specific event.
        /// </summary>
        /// <param name="context">
        ///     The source.
        /// </param>
        /// <param name="eventName">
        ///     The event name.
        /// </param>
        /// <param name="handlerName">
        ///     The handler name.
        /// </param>
        /// <param name="filter">
        ///     The filter.
        /// </param>
        public void AttachEvent(
            BindContext context,
            string eventName,
            string handlerName,
            WeakEventFilterHandler filter = null)
        {
            this.DetachEvent();

            context.SourceChanged += this.OnSourceChanged;

            this.EventName = eventName;
            this.HandlerName = handlerName;

            this.Data = context;

            object value = this.Data.GetTarget();
            if (value != null)
            {
                this.targetType = value.GetType();
                this.eventTarget = new WeakReference(value);
                this.eventAction = this.AttachEvent(eventName, handlerName, filter);
            }
        }
Esempio n. 10
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="MethodInvoker"/> class.
 /// </summary>
 /// <param name="context">
 ///     The source.
 /// </param>
 /// <param name="method">
 ///     The method.
 /// </param>
 /// <param name="parameters">
 ///     The parameters.
 /// </param>
 public MethodInvoker(BindContext context, string method, IEnumerable<BindContext> parameters)
 {
     this.Context = context;
     this.Method = method;
     this.Parameters = parameters;
 }
Esempio n. 11
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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        public void Test_BindSource_NullProperty()
        {
            var viewModel = new TestViewModel();
            var weakBindSource = new BindContext(viewModel, null);

            Assert.AreEqual(viewModel, weakBindSource.Source);
            Assert.AreEqual(null, weakBindSource.PropertyType);
            Assert.AreEqual(viewModel, weakBindSource.Value);
        }
Esempio n. 14
0
        public void Test_BindSource_NotifyValueChanged()
        {
            var viewModel = new TestViewModel();
            var bindSource = new BindContext(viewModel, "Name");

            int change = 0;

            bindSource.SourceChanged += (sender, args) => { change++; };

            bindSource.Update(true);

            Assert.AreEqual(1, change);
        }
Esempio n. 15
0
        public void Test_BindSource_NormalProperty()
        {
            var viewModel = new TestViewModel();
            var bindSource = new BindContext(viewModel, "Name");

            Assert.AreEqual(viewModel, bindSource.Source);
            Assert.AreEqual("Name", bindSource.Property);
            Assert.AreEqual(typeof(string), bindSource.PropertyType);

            Assert.AreEqual(null, bindSource.Value);

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

            Assert.AreEqual(viewModel, bindSource.OriginalSource);
            viewModel = null;
            GC.Collect();
            Assert.AreEqual(null, bindSource.OriginalSource);
        }
Esempio n. 16
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="NotifyIndexPropertyObservation"/> class.
 /// </summary>
 /// <param name="data">
 ///     The data.
 /// </param>
 /// <param name="property">
 ///     The property.
 /// </param>
 /// <param name="childProperty">
 ///     The child property.
 /// </param>
 public NotifyIndexPropertyObservation(BindContext data, string property, string childProperty)
     : base(data, property, childProperty)
 {
 }