/// <summary> /// Gets the binding source object of a binding. /// </summary> /// <param name="binding">The binding that indicates the source.</param> /// <param name="serviceProvider">Service provider provided by the framework.</param> /// <returns>The source object that is described by the binding.</returns> protected object getBindingSource(Binding binding, IServiceProvider serviceProvider) { if (binding == null) { return(null); } // Optionaly get the target object: var pvt = serviceProvider?.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; var targetObject = pvt?.TargetObject != null ? pvt.TargetObject as DependencyObject : null; // -- Case where source is explicitely provided: -- if (binding.Source != null) { // Get target object as FE if provided: if (targetObject is FrameworkElement targetAsFE) { // And if the binding source is the datacontext of the target object, then add // the object the to list of datacontexts to be followed: if (targetAsFE.IsLoaded && targetAsFE.DataContext == binding.Source) { if (!DataContextBindings.ContainsKey(binding)) { DataContextBindings.Add(binding, targetAsFE); } return(binding.Source); } // Else we cannot know now ,so report assessment when target will be loaded by setting // the current binding as 'unresolved': else if (!targetAsFE.IsLoaded) { UnresolvedBindings.Add(binding, targetAsFE); } } else { return(binding.Source); } } // -- Case where element name is provided, seek source in xaml: -- else if (!string.IsNullOrWhiteSpace(binding.ElementName) && serviceProvider != null) { return(BindingHelpers.GetSourceFromElementName(binding.ElementName, serviceProvider)); } // -- All other case where we have a target to provide: -- else if (targetObject != null) { return(getBindingSource(binding, targetObject)); // call the other method without service provider. } return(null); }
/// <summary> /// Executed when the targeted framework element is loaded. /// </summary> /// <param name="sender">The framework element that sent the event.</param> /// <param name="e">Information about the loaded event.</param> private void Unresolvedtarget_Loaded(object sender, RoutedEventArgs e) { if (sender is FrameworkElement casted) { casted.Loaded -= Unresolvedtarget_Loaded; // unsubscribe. // Applies only if extension is still alive: if (IsExtensionValid) { // Check for each unresolved binding: foreach (var item in Bindings) { // If they lie in the unresolved list, and the FE that sent the event is registered for that purpose: if (item is Binding binding && UnresolvedBindings.ContainsKey(binding) && UnresolvedBindings[binding] == casted) { // Get source (should be ok now that visual tree is constructed): if (getBindingSourcePropertyValue(item as Binding, casted) is INotifyCollectionChanged collection) { CollectionChangedEventManager.AddListener(collection, this); // add this as listener to update collection when it changes } } } // Get the opportunity to register to datacontext change event // in case we just processed a value that was unresolved and now marked in the // list of the datacontext bindings to follow: if (DataContextBindings.ContainsValue(casted)) { foreach (Binding binding in Bindings) { if (UnresolvedBindings.ContainsKey(binding)) // was unresolved { if (DataContextBindings[binding] == casted) // and is in the datacontext list to follow { casted.DataContextChanged += DatacontextTarget_DataContextChanged; } } } } // Since resolved, remove all related unresolved // bindings for the current framework element: foreach (Binding binding in Bindings) { if (UnresolvedBindings.ContainsKey(binding) && UnresolvedBindings[binding] == casted) { UnresolvedBindings.Remove(binding); } } } } }
/// <summary> /// Gets the binding source object of a binding. /// </summary> /// <param name="binding">The binding that indicates the source.</param> /// <param name="target">A target object where the source might be found.</param> /// <returns>The source object that is described by the binding.</returns> protected object getBindingSource(Binding binding, DependencyObject target) { if (binding == null) { return(null); } var targetAsFE = target as FrameworkElement; // -- Case where source is explicitely provided: -- if (binding.Source != null) { // If a target object is provided: if (targetAsFE != null) { // And if the binding source is the datacontext of the target object, then add // the object the to list of datacontexts to be followed: if (targetAsFE.IsLoaded && targetAsFE.DataContext == binding.Source) { if (!DataContextBindings.ContainsKey(binding)) { DataContextBindings.Add(binding, targetAsFE); } return(binding.Source); } // Else we cannot know now so report assessment at target loading: else if (!targetAsFE.IsLoaded) { UnresolvedBindings.Add(binding, targetAsFE); } } else { return(binding.Source); } } // -- In other cases that depends on datacontext or visual tree -- else if (target != null) { // -- Case where relative source is provided: -- if (binding.RelativeSource != null) { var relative = BindingHelpers.GetSourceFromRelativeSource(binding.RelativeSource, target); if (relative == null && targetAsFE != null && !targetAsFE.IsLoaded) { if (!UnresolvedBindings.ContainsKey(binding)) { UnresolvedBindings.Add(binding, targetAsFE); } } } // -- Case where no source is given at all: -- else if (binding.Source == null && binding.RelativeSource == null && string.IsNullOrWhiteSpace(binding.ElementName) && targetAsFE != null) { if (targetAsFE.IsLoaded) { if (!DataContextBindings.ContainsKey(binding)) { DataContextBindings.Add(binding, targetAsFE); } return(targetAsFE.DataContext); } else if (!UnresolvedBindings.ContainsKey(binding)) { UnresolvedBindings.Add(binding, targetAsFE); } } } return(null); }