Example #1
0
        /// <include file='doc\BindingContext.uex' path='docs/doc[@for="BindingContext.EnsureListManager"]/*' />
        /// <devdoc>
        ///    Create a suitable binding manager for the specified dataSource/dataMember combination.
        ///    - If one has already been created and cached by this BindingContext, return that instead.
        ///    - If the data source is an ICurrencyManagerProvider, just delegate to the data source.
        /// </devdoc>
        internal BindingManagerBase EnsureListManager(object dataSource, string dataMember)
        {
            BindingManagerBase bindingManagerBase = null;

            if (dataMember == null)
            {
                dataMember = "";
            }

            // Check whether data source wants to provide its own binding managers
            // (but fall through to old logic if it fails to provide us with one)
            //
            if (dataSource is ICurrencyManagerProvider)
            {
                bindingManagerBase = (dataSource as ICurrencyManagerProvider).GetRelatedCurrencyManager(dataMember);

                if (bindingManagerBase != null)
                {
                    return(bindingManagerBase);
                }
            }

            // Check for previously created binding manager
            //
            HashKey       key = GetKey(dataSource, dataMember);
            WeakReference wRef;

            wRef = listManagers[key] as WeakReference;
            if (wRef != null)
            {
                bindingManagerBase = (BindingManagerBase)wRef.Target;
            }
            if (bindingManagerBase != null)
            {
                return(bindingManagerBase);
            }

            if (dataMember.Length == 0)
            {
                // No data member specified, so create binding manager directly on the data source
                //
                if (dataSource is IList || dataSource is IListSource)
                {
                    // IListSource so we can bind the dataGrid to a table and a dataSet
                    bindingManagerBase = new CurrencyManager(dataSource);
                }
                else
                {
                    // Otherwise assume simple property binding
                    bindingManagerBase = new PropertyManager(dataSource);
                }
            }
            else
            {
                // Data member specified, so get data source's binding manager, and hook a 'related' binding manager to it
                //
                int    lastDot   = dataMember.LastIndexOf(".");
                string dataPath  = (lastDot == -1) ? "" : dataMember.Substring(0, lastDot);
                string dataField = dataMember.Substring(lastDot + 1);

                BindingManagerBase formerManager = EnsureListManager(dataSource, dataPath);

                PropertyDescriptor prop = formerManager.GetItemProperties().Find(dataField, true);
                if (prop == null)
                {
                    throw new ArgumentException(string.Format(SR.RelatedListManagerChild, dataField));
                }

                if (typeof(IList).IsAssignableFrom(prop.PropertyType))
                {
                    bindingManagerBase = new RelatedCurrencyManager(formerManager, dataField);
                }
                else
                {
                    bindingManagerBase = new RelatedPropertyManager(formerManager, dataField);
                }
            }

            // if wRef == null, then it is the first time we want this bindingManagerBase: so add it
            // if wRef != null, then the bindingManagerBase was GC'ed at some point: keep the old wRef and change its target
            if (wRef == null)
            {
                listManagers.Add(key, new WeakReference(bindingManagerBase, false));
            }
            else
            {
                wRef.Target = bindingManagerBase;
            }

            IntSecurity.UnmanagedCode.Demand();
            ScrubWeakRefs();
            // Return the final binding manager
            return(bindingManagerBase);
        }
Example #2
0
        internal BindingManagerBase EnsureListManager(object dataSource, string dataMember)
        {
            BindingManagerBase target = null;

            if (dataMember == null)
            {
                dataMember = "";
            }
            if (dataSource is ICurrencyManagerProvider)
            {
                target = (dataSource as ICurrencyManagerProvider).GetRelatedCurrencyManager(dataMember);
                if (target != null)
                {
                    return(target);
                }
            }
            HashKey       key       = this.GetKey(dataSource, dataMember);
            WeakReference reference = this.listManagers[key] as WeakReference;

            if (reference != null)
            {
                target = (BindingManagerBase)reference.Target;
            }
            if (target == null)
            {
                if (dataMember.Length == 0)
                {
                    if ((dataSource is IList) || (dataSource is IListSource))
                    {
                        target = new CurrencyManager(dataSource);
                    }
                    else
                    {
                        target = new PropertyManager(dataSource);
                    }
                }
                else
                {
                    int                length        = dataMember.LastIndexOf(".");
                    string             str           = (length == -1) ? "" : dataMember.Substring(0, length);
                    string             name          = dataMember.Substring(length + 1);
                    BindingManagerBase parentManager = this.EnsureListManager(dataSource, str);
                    PropertyDescriptor descriptor    = parentManager.GetItemProperties().Find(name, true);
                    if (descriptor == null)
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("RelatedListManagerChild", new object[] { name }));
                    }
                    if (typeof(IList).IsAssignableFrom(descriptor.PropertyType))
                    {
                        target = new RelatedCurrencyManager(parentManager, name);
                    }
                    else
                    {
                        target = new RelatedPropertyManager(parentManager, name);
                    }
                }
                if (reference == null)
                {
                    this.listManagers.Add(key, new WeakReference(target, false));
                    return(target);
                }
                reference.Target = target;
            }
            return(target);
        }
        internal BindingManagerBase EnsureListManager(object dataSource, string dataMember)
        {
            if (dataMember == null)
            {
                dataMember = "";
            }
            HashKey            key = GetKey(dataSource, dataMember);
            WeakReference      wRef;
            BindingManagerBase bindingManagerBase = null;

            wRef = listManagers[key] as WeakReference;
            if (wRef != null)
            {
                bindingManagerBase = (BindingManagerBase)wRef.Target;
            }
            if (bindingManagerBase != null)
            {
                return(bindingManagerBase);
            }

            // create base listManager.
            if (dataMember.Length == 0)
            {
                //IListSource so we can bind the dataGrid to a table and a dataSet
                if (dataSource is IList || dataSource is IListSource)
                {
                    bindingManagerBase = new CurrencyManager(dataSource);
                }
                else
                {
                    bindingManagerBase = new PropertyManager(dataSource);
                }

                // if wRef == null, then it is the first time we want this bindingManagerBase: so add it
                // if wRef != null, then the bindingManagerBase was GC'ed at some point: keep the old wRef and change its target
                if (wRef == null)
                {
                    listManagers.Add(key, new WeakReference(bindingManagerBase, false));
                }
                else
                {
                    wRef.Target = bindingManagerBase;
                }
                return(bindingManagerBase);
            }

            // handle relation.
            int lastDot = dataMember.LastIndexOf(".");
            BindingManagerBase formerManager = EnsureListManager(dataSource, lastDot == -1 ? "" : dataMember.Substring(0, lastDot));
            PropertyDescriptor prop          = formerManager.GetItemProperties().Find(dataMember.Substring(lastDot + 1), true);

            if (prop == null)
            {
                throw new ArgumentException(SR.GetString(SR.RelatedListManagerChild, dataMember.Substring(lastDot + 1)));
            }
            if (typeof(IList).IsAssignableFrom(prop.PropertyType))
            {
                bindingManagerBase = new RelatedCurrencyManager(formerManager, dataMember.Substring(lastDot + 1));
            }
            else
            {
                bindingManagerBase = new RelatedPropertyManager(formerManager, dataMember.Substring(lastDot + 1));
            }

            // if wRef == null, then it is the first time we want this bindingManagerBase: so add it
            // if wRef != null, then the bindingManagerBase was GC'ed at some point: keep the old wRef and change its target
            if (wRef == null)
            {
                listManagers.Add(GetKey(dataSource, dataMember), new WeakReference(bindingManagerBase, false));
            }
            else
            {
                wRef.Target = bindingManagerBase;
            }
            return(bindingManagerBase);
        }
        /// <include file='doc\BindingContext.uex' path='docs/doc[@for="BindingContext.EnsureListManager"]/*' />
        /// <devdoc>
        ///    Create a suitable binding manager for the specified dataSource/dataMember combination.
        ///    - If one has already been created and cached by this BindingContext, return that instead.
        ///    - If the data source is an ICurrencyManagerProvider, just delegate to the data source.
        /// </devdoc>
        internal BindingManagerBase EnsureListManager(object dataSource, string dataMember) {
            BindingManagerBase bindingManagerBase = null;

            if (dataMember == null)
                dataMember = "";

            // Check whether data source wants to provide its own binding managers
            // (but fall through to old logic if it fails to provide us with one)
            //
            if (dataSource is ICurrencyManagerProvider) {
                bindingManagerBase = (dataSource as ICurrencyManagerProvider).GetRelatedCurrencyManager(dataMember);

                if (bindingManagerBase != null) {
                    return bindingManagerBase;
                }
            }

            // Check for previously created binding manager
            //
            HashKey key = GetKey(dataSource, dataMember);
            WeakReference wRef;
            wRef = listManagers[key] as WeakReference;
            if (wRef != null)
                bindingManagerBase = (BindingManagerBase) wRef.Target;
            if (bindingManagerBase != null) {
                return bindingManagerBase;
            }

            if (dataMember.Length == 0) {
                // No data member specified, so create binding manager directly on the data source
                //
                if (dataSource is IList || dataSource is IListSource) {
                    // IListSource so we can bind the dataGrid to a table and a dataSet
                    bindingManagerBase = new CurrencyManager(dataSource);
                }
                else {
                    // Otherwise assume simple property binding
                    bindingManagerBase = new PropertyManager(dataSource);
                }
            }
            else {
                // Data member specified, so get data source's binding manager, and hook a 'related' binding manager to it
                //
                int lastDot = dataMember.LastIndexOf(".");            
                string dataPath = (lastDot == -1) ? "" : dataMember.Substring(0, lastDot);
                string dataField = dataMember.Substring(lastDot + 1);

                BindingManagerBase formerManager = EnsureListManager(dataSource, dataPath);

                PropertyDescriptor prop = formerManager.GetItemProperties().Find(dataField, true);
                if (prop == null)
                    throw new ArgumentException(SR.GetString(SR.RelatedListManagerChild, dataField));

                if (typeof(IList).IsAssignableFrom(prop.PropertyType))
                    bindingManagerBase = new RelatedCurrencyManager(formerManager, dataField);
                else
                    bindingManagerBase = new RelatedPropertyManager(formerManager, dataField);
            }

            // if wRef == null, then it is the first time we want this bindingManagerBase: so add it
            // if wRef != null, then the bindingManagerBase was GC'ed at some point: keep the old wRef and change its target
            if (wRef == null)
                listManagers.Add(key, new WeakReference(bindingManagerBase, false));
            else
                wRef.Target = bindingManagerBase;

            IntSecurity.UnmanagedCode.Demand();
            ScrubWeakRefs();
            // Return the final binding manager
            return bindingManagerBase;
        }
 internal BindingManagerBase EnsureListManager(object dataSource, string dataMember)
 {
     BindingManagerBase target = null;
     if (dataMember == null)
     {
         dataMember = "";
     }
     if (dataSource is ICurrencyManagerProvider)
     {
         target = (dataSource as ICurrencyManagerProvider).GetRelatedCurrencyManager(dataMember);
         if (target != null)
         {
             return target;
         }
     }
     HashKey key = this.GetKey(dataSource, dataMember);
     WeakReference reference = this.listManagers[key] as WeakReference;
     if (reference != null)
     {
         target = (BindingManagerBase) reference.Target;
     }
     if (target == null)
     {
         if (dataMember.Length == 0)
         {
             if ((dataSource is IList) || (dataSource is IListSource))
             {
                 target = new CurrencyManager(dataSource);
             }
             else
             {
                 target = new PropertyManager(dataSource);
             }
         }
         else
         {
             int length = dataMember.LastIndexOf(".");
             string str = (length == -1) ? "" : dataMember.Substring(0, length);
             string name = dataMember.Substring(length + 1);
             BindingManagerBase parentManager = this.EnsureListManager(dataSource, str);
             PropertyDescriptor descriptor = parentManager.GetItemProperties().Find(name, true);
             if (descriptor == null)
             {
                 throw new ArgumentException(System.Windows.Forms.SR.GetString("RelatedListManagerChild", new object[] { name }));
             }
             if (typeof(IList).IsAssignableFrom(descriptor.PropertyType))
             {
                 target = new RelatedCurrencyManager(parentManager, name);
             }
             else
             {
                 target = new RelatedPropertyManager(parentManager, name);
             }
         }
         if (reference == null)
         {
             this.listManagers.Add(key, new WeakReference(target, false));
             return target;
         }
         reference.Target = target;
     }
     return target;
 }