gpt4 book ai didi

c# - .Net 如何为用户控件的属性设置 IsReadOnly

转载 作者:太空宇宙 更新时间:2023-11-03 18:54:43 24 4
gpt4 key购买 nike

我在 .NET 中有一个具有 2 个新属性的用户控件

Prop1: Boolean
Prop2: String

当用户将 Prop1 设置为 false 时,我想在属性网格中使 Prop2 READONLY

enter image description here

最佳答案

如果你想根据某些标准在运行时使属性外观为只读(灰色),你需要分配一个 CustomTypeDescriptor到您的类(class),它为属性网格提供有关您的类(class)的元数据。

PropertyGrid控件使用对象的类型描述符来提取有关其属性的信息以显示。类型描述符,返回 PropertyDescriptor 的列表对象作为属性列表。每个 PropertyDescriptor 都包含一些方法和属性,用于返回显示名称、描述和其他有关属性的信息。 PropertyDescriptorIsReadOnly 属性负责通知 PropertyGrid 该属性是否为只读。

示例

在下面的示例中,我创建了一个包含两个属性的类。 EditableStringProperty。如果 EditabletrueStringProperty 是可编辑的,否则它将是只读的并且在 PropertyGrid< 中显示为灰色.

我的属性描述符

它负责为属性提供元数据。在实现此类时,对于大多数属性,我们将使用使用原始属性实现的简单实现,但对于 IsReadOnly,我们将根据 Editable 属性的值来决定所有者对象:

using System;
using System.ComponentModel;
using System.Linq;
public class MyPropertyDescriptor : PropertyDescriptor
{
PropertyDescriptor p;
SampleClass o;
public MyPropertyDescriptor(PropertyDescriptor originalProperty, SampleClass owenr)
: base(originalProperty) { p = originalProperty; o = owenr; }
public override bool CanResetValue(object component)
{ return p.CanResetValue(component); }
public override object GetValue(object component) { return p.GetValue(component); }
public override void ResetValue(object component) { p.ResetValue(component); }
public override void SetValue(object component, object value)
{ p.SetValue(component, value); }
public override bool ShouldSerializeValue(object component)
{ return p.ShouldSerializeValue(component); }
public override AttributeCollection Attributes { get { return p.Attributes; } }
public override Type ComponentType { get { return p.ComponentType; } }
public override bool IsReadOnly { get { return !o.Editable; } }
public override Type PropertyType { get { return p.PropertyType; } }
}

MyTypeDescriptor

它负责提供对象的属性列表。对于我们将在运行时更改其行为的 StringProperty,我们将返回一个 MyPropertyDescriptor:

using System;
using System.ComponentModel;
using System.Linq;
public class MyTypeDescriptor : CustomTypeDescriptor
{
ICustomTypeDescriptor d;
SampleClass o;
public MyTypeDescriptor(ICustomTypeDescriptor originalDescriptor, SampleClass owner)
: base(originalDescriptor) { d = originalDescriptor; o = owner; }
public override PropertyDescriptorCollection GetProperties()
{ return this.GetProperties(new Attribute[] { }); }
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>()
.Select(p => p.Name == "StringProperty" ? new MyPropertyDescriptor(p, o) : p)
.ToArray();
return new PropertyDescriptorCollection(properties);
}
}

MyTypeDescriptionProvider

当有人(如属性网格)请求类型描述时,它负责为您的对象返回类型描述符:

using System;
using System.ComponentModel;
public class MyTypeDescriptionProvider : TypeDescriptionProvider
{
public MyTypeDescriptionProvider()
: base(TypeDescriptor.GetProvider(typeof(object))) { }

public override ICustomTypeDescriptor GetTypeDescriptor(Type type, object o)
{
ICustomTypeDescriptor baseDescriptor = base.GetTypeDescriptor(type, o);
return new MyTypeDescriptor(baseDescriptor, (SampleClass)o);
}
}

示例类

最后是类的实现:

using System;
using System.ComponentModel;
[TypeDescriptionProvider(typeof(MyTypeDescriptionProvider))]
public class SampleClass
{
[RefreshProperties(RefreshProperties.All)]
public bool Editable { get; set; }
string sp;
public string StringProperty
{
get { return sp; }
set
{
if (Editable)
sp = value;
}
}
}

结果

enter image description here

进一步阅读

您可以在以下帖子中阅读其他一些解决方案:

关于c# - .Net 如何为用户控件的属性设置 IsReadOnly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48360043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com