Am having a class as following,
class Person { private string _PersonName; [Category("General")] [ReadOnly(true)] [Browsable(true)] public string Name { get { return _PersonName; } set { _PersonName= value; } } } and an ObservableCollection of Person objects as following,
ObservableCollection<Person> Persons = new ObservableCollection<Person>(); Consider, I am having 5 Person objects in Persons(ObservableCollection).
Person Person1 = new Person(){Name = "User 1";} Person Person2 = new Person(){Name = "User 2";} Person Person3 = new Person(){Name = "User 3";} Person Person4 = new Person(){Name = "User 4";} Person Person5 = new Person(){Name = "User 5";} Persons.Add(Person1); Persons.Add(Person2); Persons.Add(Person3); Persons.Add(Person4); Persons.Add(Person5); Now, am changing Browsable attribute value of Second object from true to false dynamically as follows,
PropertyDescriptor pd = TypeDescriptor.GetProperties(Person1)["Name"]; BrowsableAttribute ba =(BrowsableAttribute)pd.Attributes[typeof(BrowsableAttribute)]; FieldInfo IsBrowsable = ba.GetType().GetField("browsable", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic); IsBrowsable.SetValue(ba, false); after this change, value of Browsable(true) attribute of all other objects becomes false.
This is the problem, How to prevent this?
Thank you all.
Persons.Add(new Person().Name = "User5");doesn't look right at all. Instead tryPersons.Add(new Person() { Name = "User5" });