示例#1
0
        /// <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);
        }