Question

List in Property

Posted 4 years ago by Avatar Eli Obadia
I have a property that is a list of Values I want to allow the user to delete but not to add, how can I take out the "+" icon. I have this property readonly with a custom editor that opens a dialog in the custom editor I progarmmatically add items to the list but I don't want the user to add by clicking "+" I only want to allow him removing items with the "-", how can I do it?

Thanks,
Eli

Comments (6)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Eli,

You would need to create a derivation of ExpandableCollectionConverter that overrides CanAddItem and always returns false. Then you have to use the TypeConverterAttribute which is passed your custom converter's type, either on your collection's class definition or on the associated property.

The Collection Null Values QuickStart under the PropertyGrid section of our Sample Browser shows an example similar to this.

If you are simply using a type like List<MyType>, then you can't really add the attribute there. The best solution in this case is generally to add a custom class like "public class MyTypeCollection : List<MyType>" and add the attribute there. This ensures that your custom converter is picked up anywhere the collection is exposed.

If you only have 1 property whose behavior you want to customize (i.e. remove the add functionality), then creating a custom list class is probably overkill.

Actipro Software Support
Posted 4 years ago by Eli Obadia
Thanks but my property was set up ReadOnly(true) and the propertycontrol does not allow me to change the content however is allowing to add and remove items, is that a bug? Look at the code is very simple.

    public List<ColumnValueList> _ColumnValueList = new List<ColumnValueList>();

    [ReadOnly(true)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [Category("Lista")]
    public class ColumnValueList
    {
        private string _Title = "";

        /// <summary>
        /// Gets or sets the last name of the person.
        /// </summary>
        /// <value>The last name of the person.</value>
        [DefaultValue(null)]
        [Category("Valores")]
        [Description("IndicatesValores.")]
        [DisplayName("Column")]
        [ReadOnly(true)]
        [NotifyParentProperty(true)]
        public string Title
        {
            get
            {
                return this._Title;
            }
            set
            {
                if (value != this._Title)
                    this._Title = value;
            }
        }

        public override string ToString()
        {
            return (this._Title);
        }
    }
Or if not maybe I can just "return" false here to avoid deleting the item:
private void OnPropertyGridPropertyChildAdding(object sender, PropertyDataAccessorChildAddingRoutedEventArgs e) {

        }
The question is how I return "false" from OnPropertyGridPropertyChildAdding, is there any parameter in which I can return "false" to stop the adding.

Thanks a lot,
Eli


[Modified at 10/15/2010 01:02 PM]
Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Eli,

I'm not sure the ReadOnlyAttribute on your class type won't have any affect. It certainly wouldn't prevent the list from adding/removing items.

If you are handling the PropertyChildAdding event, then you can set e.Cancel to true to stop it from adding a child. But this won't hide the add button.

To hide the add button, you need to add a custom TypeConverter to you list as I described above. Again, the Collection Null Values QuickStart under the PropertyGrid section of our Sample Browser shows an example similar to this. Specifically, the "StringListConverter" is used to allow null values to be added to the list. But, your converter would always return false.

If this doesn't help, please put together a small sample project and email it over to our support address. Once we have that, we can take a closer look.

Actipro Software Support
Posted 4 years ago by Eli Obadia
OK, I can do this option you suggest "If you are handling the PropertyChildAdding event, then you can set e.Cancel to true to stop it from adding a child. But this won't hide the add button."

But e does not have a field Cancel ? what do you mean by making "e.Cancel = false", remember that e type is PropertyDataAccessorChildAddedRoutedEventArgs
Maybe I have to do some casting?

Tahnsk,
Eli
Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Eli,

There are two events: PropertyChildAdding and PropertyChildAdded. The former has a Cancel property, while the latter does not (because the child has already been added).

Actipro Software Support
Posted 4 years ago by Eli Obadia
Oops!

Greats

Thanks a lot!
Information The latest build of this product (2014.2 build 0610) was released 1 month ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.