6

我有一个PropertyGrid用来在帮助类中显示属性的。我将助手类分配给PropertyGrid这样的:

myPropertyGrid.SelectedObject = mySettingsHelper;

在助手类中,我ReadOnlyAttribute在设计时分配如下:

[DisplayName("DisplayExA"),
Description("DescriptionExA"),
ReadOnlyAttribute(true)]
public string PropertyA { get; set; }

[DisplayName("DisplayExB"),
Description("DescriptionExB"),
ReadOnlyAttribute(false)]
public string PropertyB { get; set; }

[DisplayName("DisplayExC"),
Description("DescriptionExC"),
ReadOnlyAttribute(true)]
public string PropertyC { get; set; }

但现在我需要能够在运行时动态更改各个属性的此属性。根据某些标准,其中一些属性可能需要只读或非只读。如何在运行时动态进行更改?

编辑:

我尝试了以下代码,但这为对象的每个实例设置了 ReadOnly 属性!我想按对象做。有时,一个对象的 PropertyA 可能是只读的,而第二个对象的 PropertyA 可能不是只读的。

public static class PropertyReadOnlyHelper
{
    public static  void SetReadOnly(object container, string name, bool value)
    {
        try
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(container.GetType())[name];
            ReadOnlyAttribute attribute = (ReadOnlyAttribute)descriptor.Attributes[typeof(ReadOnlyAttribute)];
            FieldInfo fieldToChange = attribute.GetType().GetField("isReadOnly",
                                                System.Reflection.BindingFlags.NonPublic |
                                                System.Reflection.BindingFlags.Instance);
            fieldToChange.SetValue(attribute, value);
        }
        catch { }
    }
}
4

5 回答 5

1

使用此CodeProject文章中的库,我能够完全满足我的需要(只读属性的对象级分配) 。好的是它使我仍然可以使用 .NETPropertyGrid并且只使用自定义属性来处理动态设置。

于 2013-10-29T17:40:48.760 回答
0

使用反射获取ReadOnlyAttribute类的实例引用,然后切换该IsReadOnly实例的属性。最后,如果需要,通过将其 SelectedObjects 设置为 null 然后重新设置它来重新选择 PropertyGrid 中的项目。您也可以使用 PropertyGridRefreshTabs方法来执行此操作,我不确定。

编辑:

不幸的是 IsReadOnly 属性本身是只读的……在这种情况下,我们必须使用反射来更改 IsReadOnly 属性的支持字段的值。

于 2013-10-28T17:23:09.167 回答
0

添加只读

TextBoxID.Attributes.Add("readonly","true");

删除只读

TextBoxID.Attributes.Remove("readonly");
于 2017-06-21T11:30:56.020 回答
0

在 PropertyGrid 中动态设置属性的可浏览只读属性通常需要一起使用,它们也是类似的工作

经过几次接触,Reza Aghaei关于“在运行时隐藏 PropertyGrid 中的一些属性”的精彩回答也适用于操作只读属性。

public class CustomObjectWrapper : CustomTypeDescriptor
{
    public object WrappedObject { get; private set; }
    public List<string> BrowsableProperties { get; private set; }
    public List<string> ReadonlyProperties { get; private set; }

    public CustomObjectWrapper(object o)
        : base(TypeDescriptor.GetProvider(o).GetTypeDescriptor(o))
    {
        WrappedObject = o;
        BrowsableProperties = new List<string>() { "Text", "BackColor" };
        ReadonlyProperties = new List<string>() { "Font" };
    }
    public override PropertyDescriptorCollection GetProperties()
    {
        return this.GetProperties(new Attribute[] { });
    }
    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        List<PropertyDescriptor> result = new List<PropertyDescriptor>();

        IEnumerable<PropertyDescriptor> properties = base.GetProperties(attributes).Cast<PropertyDescriptor>()
            .Where(p => BrowsableProperties.Contains(p.Name));//unbrowsable filtering

        foreach (var p in properties)
        {
            PropertyDescriptor resultPropertyDescriptor = null;

            //handle being readonly 
            if (ReadonlyProperties.Contains(p.Name))
            {
                List<Attribute> atts = p.Attributes.Cast<Attribute>().ToList();
                atts.RemoveAll(a => a.GetType().Equals(typeof(ReadOnlyAttribute)));//remove any readonly attribute
                atts.Add(new ReadOnlyAttribute(true));//add "readonly=true" attribute

                resultPropertyDescriptor = TypeDescriptor.CreateProperty(WrappedObject.GetType(), p, atts.ToArray());
            }
            else
            {
                resultPropertyDescriptor = TypeDescriptor.CreateProperty(WrappedObject.GetType(), p, p.Attributes.Cast<Attribute>().ToArray());
            }

            if (resultPropertyDescriptor != null)
                result.Add(resultPropertyDescriptor);

        }

        return new PropertyDescriptorCollection(result.ToArray());
    }
}

和用法:

propertyGrid1.SelectedObject = new CustomObjectWrapper(myobject);
于 2020-06-19T08:19:20.910 回答
0
Please try the code below.


  
[CategoryAttribute("2. LINE"), DisplayNameAttribute("Spline Line Tension"),
 DescriptionAttribute("Chart's Spline Line Tension "), ReadOnlyAttribute(false)]
public float _PG_SplineTension
{
    get
    {
        bool lbReadyOnly = true;
        SetPropertyReadOnly("_PG_SplineTension", lbReadyOnly);
        return this.cfSplineTension;
   }
    set { this.cfSplineTension = value; }
}



private void SetPropertyReadOnly(string lsProperty, bool lbIsReadOnly)
{
    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())[lsProperty];
    ReadOnlyAttribute attribute = (ReadOnlyAttribute)

    descriptor.Attributes[typeof(ReadOnlyAttribute)];
    FieldInfo fieldToChange = attribute.GetType().GetField("isReadOnly",
        System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance);
    fieldToChange.SetValue(attribute, lbIsReadOnly);
}
于 2021-01-10T00:02:12.293 回答