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); }
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); }
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); }
/// <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); }
/// <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)); }
/// <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; }
/// <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) { }
/// <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); }
/// <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); } }
/// <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; }
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); }
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); }
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); }
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); }
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); }