Question

I have a list that it used to Property Grid, I want to eliminate the possibility of adding via button - Add.

But I want to be able to edit the data already are.

My list:

    private List<Pos> _position = new List<Pos>();

    public List<Pos> Position
    {
        get { return _position; }
        set
        {
            _position = value;
            NotifyPropertyChanged("Position");
        }
    }

Pos.cs:

public class Pos
{
    public string Name { get; set; }
    public double Postion { get; set; }

    public Pos()
        : this(null, Double.NaN)
    {

    }

    public Pos(string name, double postion)
    {
        this.Name = name;
        this.Postion = postion;
    }
}

I tried to put [ReadOnly(true)] above list, it still gives the option of adding.

Does anyone have an idea how to do it?

Was it helpful?

Solution

I canceled the option to add / delete, like this:

I created a generic class:

public class PosList<T> : List<T>, ICollection<T>, IList
{

    public ValuesList(IEnumerable<T> items) : base(items) { }

    bool ICollection<T>.IsReadOnly { get { return true; } }

    bool IList.IsReadOnly { get { return true; } }

}

That gives the possibility to Add/Remove ​​in code, but not through the collection editor.

use:

private PosList<Pos> _position = new PosList<Pos>(new List<Pos>());

public PosList<Pos> Position
{
    get { return _position; }
    set
    {
        _position = value;
        NotifyPropertyChanged("Position");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top