locked
PersistenceMode.InnerDefaultProperty issue RRS feed

  • Question

  • User-1690232954 posted

    So I'm trying to create a custom control which inherits from DropDownList.

    However, I would like to essentially suppress the Items collection as the inner default property of the control and use my own. This doesn't seem to be too incredibly difficult to do, but I'm experiencing some unusual behavior for which I am unable to find a solution.

    My code is essentially this:

    [DefaultProperty("MyItems")]
    [ParseChildren(true, "MyItems")]
    public class MyCustomDropDownList : DropDownList
    {
       [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
       public MyItemCollection MyItems
       {
          get { ... }
       }
    
       [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
       public override Items
       {
          get { ... }
       }
    }
    
    public class MyItemCollection : IList<MyItem>, ICollection<MyItem>, IEnumerable<MyItem>, IStateManager
    {
       ...
    }
    
    public class MyItem
    {
       ...
    }


    The problem is that in the Source view on my page, if I attempt to add a MyCustomDropDownList control, the Intellisense for the inner property displays:

    xxx:MyItem
    MyItems

    How do I get rid of the "MyItems" tag? I only want it to show "xxx:MyItem" as if I were using a normal list control.

    Friday, November 13, 2009 3:08 PM

Answers

  • User-2106054853 posted

    Hi,

    To achieve this effect we need to let the collection class inherit from IList. See below sample:


    [ParseChildren(true, "MyItems")]
    public class MyCustomDropDownList : DropDownList
    {

       [DefaultValue((string) null), PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public MyListItemCollection MyItems
       {
          get;set;
       }

       [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
       public override ListItemCollection Items
       {
           get
           {
               return base.Items;
           }
       }

    }

    public sealed class MyListItemCollection : IList
    {
     
        // Methods
     
        public MyItem this[int index]
        {
            get
            {

                throw new NotImplementedException();

            }
            set
            {
                throw new NotImplementedException();
            }
        }
     
        object IList.this[int index]
        {
            get
            {
                throw new NotImplementedException();

            }
            set
            {
                throw new NotImplementedException();

            }
        }

     

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsSynchronized
        {
            get { throw new NotImplementedException(); }
        }

        public object SyncRoot
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IList Members

        public int Add(object value)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(object value)
        {
            throw new NotImplementedException();
        }

        public int IndexOf(object value)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, object value)
        {
            throw new NotImplementedException();
        }

        public bool IsFixedSize
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public void Remove(object value)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

     

    public class MyItem
    {
     
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 16, 2009 3:53 AM

All replies

  • User-2106054853 posted

    Hi,

    To achieve this effect we need to let the collection class inherit from IList. See below sample:


    [ParseChildren(true, "MyItems")]
    public class MyCustomDropDownList : DropDownList
    {

       [DefaultValue((string) null), PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public MyListItemCollection MyItems
       {
          get;set;
       }

       [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
       public override ListItemCollection Items
       {
           get
           {
               return base.Items;
           }
       }

    }

    public sealed class MyListItemCollection : IList
    {
     
        // Methods
     
        public MyItem this[int index]
        {
            get
            {

                throw new NotImplementedException();

            }
            set
            {
                throw new NotImplementedException();
            }
        }
     
        object IList.this[int index]
        {
            get
            {
                throw new NotImplementedException();

            }
            set
            {
                throw new NotImplementedException();

            }
        }

     

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            throw new NotImplementedException();
        }

        public int Count
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsSynchronized
        {
            get { throw new NotImplementedException(); }
        }

        public object SyncRoot
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IList Members

        public int Add(object value)
        {
            throw new NotImplementedException();
        }

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public bool Contains(object value)
        {
            throw new NotImplementedException();
        }

        public int IndexOf(object value)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, object value)
        {
            throw new NotImplementedException();
        }

        public bool IsFixedSize
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public void Remove(object value)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

     

    public class MyItem
    {
     
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 16, 2009 3:53 AM
  • User-1690232954 posted

    Very good. Implementing the IList interface removed the additional tag from Intellisense. Curious that this would work correctly with IList interface but not with the generic IList.

    Oh well, thanks for the help!

    Monday, November 16, 2009 4:19 PM