public void Bind(object source, PropertyInfo piSource, object destination, PropertyInfo piDest, IBinderConverter converter, SynchronizationContext applyBindingContext) { weakSrc = new WeakReference(source); weakDst = new WeakReference(destination); _Converter = converter; _PropNameSource = piSource.Name; _PropNameDest = piDest.Name; gethandler = GetSetUtils.CreateGetHandler <TValueSource>(piSource); //le set handler devrait être sur le type destination if (_Converter != null) { _CurrentChanged = OnValueChanged1; sethandlerDest = GetSetUtils.CreateSetHandler <TValueDest>(piDest); } else { _CurrentChanged = OnValueChanged; sethandler = GetSetUtils.CreateSetHandler <TValueSource>(piDest); } DataBinder.AddNotify <TValueSource>(source, piSource.Name, gethandler, _CurrentChanged, applyBindingContext); }
void OnValueChanged1(TValueSource value) { if (weakDst != null && weakDst.IsAlive) { if (_Converter != null) { IBinderConverter <TValueDest, TValueSource> cv = _Converter as IBinderConverter <TValueDest, TValueSource>; if (cv == null) { throw new Exception(string.Format("converter must implement 'IBinderConverter<{0},{1}>'", typeof(TValueDest), typeof(TValueSource))); } try { sethandlerDest(weakDst.Target, cv.Convert(value)); } catch (Exception ex) { throw new CompiledBindingException(string.Format("Set value on property '{0}' failed", _PropNameDest), ex); } } } }
public static void AddCompiledBinding(object source, string propertypath, object destination, string destPropertypath, SynchronizationContext applyBindingContext, IBinderConverter converter) { Check.IsNotNull("source", source); Check.IsNotNull("propertypath", propertypath); Check.IsNotNull("destination", destination); Check.IsNotNull("destPropertypath", destPropertypath); string[] pathItems = propertypath.Split('.'); PropertyPathBindingItem item = new PropertyPathBindingItem(source, propertypath, destination, destPropertypath, converter, applyBindingContext); _Bindings.Add(item); }
/// <summary> /// constructeur par défaut /// </summary> /// <param name="source"></param> /// <param name="propertyPath"></param> /// <param name="destination"></param> /// <param name="propertyPathdest"></param> /// <param name="converter"></param> internal PropertyPathBindingItem(object sourceTarget, string propertyPath, object destinationTarget, string propertyPathdest, IBinderConverter converter, SynchronizationContext applyBindingContext) { _ApplyBindingContext = applyBindingContext; _Converter = converter; _DestinationBinding = new OnePropertyPathBinding(); _SourceBinding = new OnePropertyPathBinding(); _DestinationBinding.Bind(destinationTarget, propertyPathdest, delegate(int currentIndex, EqualityWeakReference weakSource) { //this delegate it's : "What to do when an intermediate property changing" return(delegate(object value) { if (weakSource.IsAlive) { _DestinationBinding.RemoveNotify(currentIndex); _DestinationBinding.BindPropertyPath(value, currentIndex); if (_CurrentBinding != null) { RecreateRealBinding(); } else { //rebind all level from source _SourceBinding.RemoveNotify(0); _SourceBinding.BindPropertyPath(this.Source, 0); } } }); }, null //delegate(int index, PropertyInfo pi, object src) //{ // ////Cas de la propriété à bindée // //EqualityWeakReference weakDest = new EqualityWeakReference(src); // //PathItem p = _DestinationBinding.Items[index]; // //p.Source = weakDest; //} ); ///Connect to the source _SourceBinding.Bind(sourceTarget, propertyPath, delegate(int currentIndex, EqualityWeakReference weakSource) { //this delegate it's : "What to do when an intermediate property changing" return(delegate(object value) { if (weakSource.IsAlive) { _SourceBinding.RemoveNotify(currentIndex); InternalUnBind(); _SourceBinding.BindPropertyPath(value, currentIndex); } }); }, //this delegate it's : "What to do when the final property changing" delegate(int index, PropertyInfo pi, object src) { RecreateRealBinding(); }); }