将 ICustomTypeDescriptor.GetProperties 返回的属性动态更改为只读
我有一个实现 ICustomTypeDescriptor 的类,并由用户在 PropertyGrid 中查看和编辑。我的类还有一个 IsReadOnly 属性,它确定用户稍后是否能够保存更改。如果用户无法保存,我不想允许他们进行更改。因此,如果 IsReadOnly 为 true,我想覆盖任何可在属性网格中编辑的属性。
我尝试使用 ICustomTypeDescriptor 的 GetProperties 方法向每个 PropertyDescriptor 添加 ReadOnlyAttribute。但它似乎不起作用。这是我的代码。
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
List<PropertyDescriptor> fullList = new List<PropertyDescriptor>();
//gets the base properties (omits custom properties)
PropertyDescriptorCollection defaultProperties = TypeDescriptor.GetProperties(this, attributes, true);
foreach (PropertyDescriptor prop in defaultProperties)
{
if(!prop.IsReadOnly)
{
//adds a readonly attribute
Attribute[] readOnlyArray = new Attribute[1];
readOnlyArray[0] = new ReadOnlyAttribute(true);
TypeDescriptor.AddAttributes(prop,readOnlyArray);
}
fullList.Add(prop);
}
return new PropertyDescriptorCollection(fullList.ToArray());
}
这是否是使用 TypeDescriptor.AddAttributes() 的正确方法?在调用后进行调试时,AddAttributes() 属性仍然具有相同数量的属性,其中没有一个是 ReadOnlyAttribute。
I have a class which implements ICustomTypeDescriptor, and is viewed and edited by the user in a PropertyGrid. My class also has a IsReadOnly property which determines if the user will be able to save their changes later. I don't want to allow the user to make changes if they will not be able to save. So if IsReadOnly is true I want to override any properties that would otherwise be editable to be read-only in a property grid.
I'm trying to use the GetProperties method of ICustomTypeDescriptor to add a ReadOnlyAttribute to each PropertyDescriptor. But it doesn't seem to be working. Here's my code.
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
List<PropertyDescriptor> fullList = new List<PropertyDescriptor>();
//gets the base properties (omits custom properties)
PropertyDescriptorCollection defaultProperties = TypeDescriptor.GetProperties(this, attributes, true);
foreach (PropertyDescriptor prop in defaultProperties)
{
if(!prop.IsReadOnly)
{
//adds a readonly attribute
Attribute[] readOnlyArray = new Attribute[1];
readOnlyArray[0] = new ReadOnlyAttribute(true);
TypeDescriptor.AddAttributes(prop,readOnlyArray);
}
fullList.Add(prop);
}
return new PropertyDescriptorCollection(fullList.ToArray());
}
Is this even the correct way to use TypeDescriptor.AddAttributes()? While debuging after the call the AddAttributes() prop still has the same number of attributes none of which are a ReadOnlyAttribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TypeDescriptor.AddAttributes
将类级属性添加到给定对象或对象类型,而不是属性级属性。最重要的是,我认为除了返回的 TypeDescriptionProvider 的行为之外,它没有任何影响。相反,我会像这样包装所有默认属性描述符:
其中 ReadOnlyWrapper 是这样的类:
TypeDescriptor.AddAttributes
adds class-level attributes to a given object or object type, not property-level attributes. On top of that, I don't think it has any effect other than in the behavior of the returnedTypeDescriptionProvider
.Instead, I would wrap all the default property descriptors like this:
where
ReadOnlyWrapper
is a class like this: