更改ObservableCollection中对象的属性值会覆盖同一集合中其他对象的属性值

时间:2015-08-11 09:28:01

标签: c# wpf c#-4.0

我的课程如下,

class Person
{
   private string _PersonName;

   [Category("General")]
   [ReadOnly(true)]
   [Browsable(true)]
   public string Name
   {
       get { return _PersonName; }
       set { _PersonName= value; }
   }
}

ObservableCollectionPerson个对象如下,

ObservableCollection<Person> Persons =  new ObservableCollection<Person>();

请注意,我在Person中有5个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);

现在,我正在动态地将第二个对象的Browsable属性值从true更改为false

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);

此更改后,所有其他对象的Browsable(true)属性值变为false

这是问题,如何防止这种情况?

谢谢大家。

0 个答案:

没有答案