Example #1
0
        protected Binding CreateTheBinding(BindingTarget bindingTarget, MyBindingInfo bInfo, Type sourceType,
                                           OSCollection pathListeners, out bool isCustom)
        {
            System.Diagnostics.Debug.Assert(sourceType != null, "The SourceType should never be null here.");

            // One node:    1 parent, 0 intervening objects, the terminal path element is "." -- The binding binds to the source data itself.
            // Two nodes:   1 parent, 0 intervening objects, 1 terminal path element.
            // Three nodes: 1 parent, 1 intervening object, 1 terminal path element.
            // Four nodes:  1 parent, 2 intervening objects, 1 terminal path element.

            // The next to last node, must have data, in order to avoid binding warnings.
            ObservableSource           lastParent       = pathListeners[pathListeners.Count - 2];
            string                     strNewPath       = pathListeners.GetNewPath(justForDiag: false);
            ObservableSourceStatusEnum lastParentStatus = lastParent.Status;

            System.Diagnostics.Debug.WriteLine($"Path = {bInfo.PropertyPath.Path}, NewPath = {strNewPath}, " +
                                               $"Terminal Node Status = {lastParentStatus}.");

            if (lastParentStatus.IsReadyOrWatching())
            {
                // Check to see if DataContext holds source property.
                // When a DataContext is set via a binding, in some cases,
                // bindings that rely on that DataContext may get set
                // before the binding that provides the correct DataContext is set.

                ObservableSource root = pathListeners[ROOT_INDEX];
                if (root.SourceKind == SourceKindEnum.FrameworkElement || root.SourceKind == SourceKindEnum.FrameworkContentElement)
                {
                    string firstChildPathElement = pathListeners[ROOT_INDEX + 1].PathElement;
                    if (!root.DoesChildExist(firstChildPathElement))
                    {
                        System.Diagnostics.Debug.WriteLine($"No Binding is being created. Data is present, but doesn't contain source property: {firstChildPathElement}.");
                        isCustom = false;
                        return(null);
                    }
                }

                // TODO: What about the original PropertyPath parameters?
                PropertyPath newPath = new PropertyPath(strNewPath);

                isCustom = lastParent.IsPropBagBased; // .IsPropBagBasedAndNoCustomTypeDescriptors;
                return(CreateBinding(bindingTarget, bInfo, sourceType, newPath, isCustom));
            }
            else
            {
                System.Diagnostics.Debug.Assert(pathListeners[0].IsListeningForNewDC, "There are no active listeners!");

                System.Diagnostics.Debug.WriteLine("No Binding is being created, not enough data.");
                isCustom = false;
                return(null);
            }
        }