带PropertyGrid的类属性的ReadOnly属性的帮助 [英] Help with ReadOnly attribute of a class property w/ PropertyGrid

查看:108
本文介绍了带PropertyGrid的类属性的ReadOnly属性的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

与PropertyGrid一起使用时,我对类属性的ReadOnly属性有疑问.

简而言之,我创建两个具有各自属性网格的表单,并将SelectedObject分配给List< myclass>.元素.在MyClass中,我有两个属性,Enabled和AString.如果Enabled为false,则AString为ReadOnly.如果Enabled为true,则AString为Read/Write.我遇到的问题是,对Form1 propertygrid的AString属性的ReadOnly属性所做的任何更改也都可以通过Form2 propertygrid的AString属性看到.

我在设置ReadOnly属性时做错了吗?如何解决此问题,以便可以基于用户与Propertygrid的交互来启用/禁用propertygrid中显示的属性.

这是显示我面临的问题的示例代码:

Morning All,

I have a question about the ReadOnly attribute of a class property when used with the PropertyGrid.

Long story short, I create two forms each with their own propertygrid and assign the SelectedObject to a List<myclass> element. In MyClass I have two properties, Enabled and AString. When Enabled is false, AString is ReadOnly. When Enabled is true, AString is Read/Write. The problem I''m having is any changes to the ReadOnly attribute of the AString property of the Form1 propertygrid is also being seen by the AString property of the Form2 propertygrid.

Am I doing something wrong to set the ReadOnly attribute? How would I fix this issue so that I can Enable/Disable properties shown in the propertygrid based on user interaction with the Propertygrid.

Here is sample code showing the problem I''m facing:

namespace PropertygridTestApp
{
    public partial class Form1 : Form
    {
        List<testclass> TestClasses = new List<testclass>();
        public Form1()
        {
            InitializeComponent();
            TestClasses.Add(new TestClass());
            TestClasses.Add(new TestClass());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form aForm = new Form2();
            aForm.Tag = TestClasses[0];
            aForm.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form aForm = new Form2();
            aForm.Tag = TestClasses[1];
            aForm.Show();
        }
    }
}

namespace PropertygridTestApp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = (this.Tag as TestClass);
        }
    }
}

namespace PropertygridTestApp
{
    class TestClass
    {
        private bool _enabled;
        private string _aString;

        public TestClass()
        {
            _enabled = false;
            _aString = "Test String";
        }

        [Browsable(true),
        ReadOnly(false),
        RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public bool Enabled
        {
            get { return (_enabled); }
            set { 
                _enabled = value;
                PropertyDescriptor Desc = TypeDescriptor.GetProperties(this)["AString"];
                ReadOnlyAttribute  att  = (Desc.Attributes[typeof(ReadOnlyAttribute)] as ReadOnlyAttribute);
                FieldInfo          fi   = att.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
                fi.SetValue(att, !_enabled);
            }
        }

        [Browsable(true),
        ReadOnly(true)]
        public string AString
        {
            get { return (_aString); }
            set { _aString = value; }
        }
    }
}
</testclass></testclass>



任何帮助将不胜感激!
Aaron



Any help would be appreciated!
Aaron

推荐答案

以下是创建只读属性的方法:
Here is how you create a readonly property:
public string AString 
{
   get 
   {
      return "Some value";
   }  
}



上面的属性只有一个"get"(省略"set"),这意味着没有人可以更改该值,因此它是只读的.如果要限制谁看到您的财产,请根据需要尝试将其保护或私有,而不是公开.

祝您编程愉快,
Morgan



Above, the property only has a "get" (ommiting the "set"), this mean no one can change the value, hence it is readonly. If you want to restrict who sees your property, try making it protected or private if need be, not public.

Happy coding,
Morgan


您无法在运行时更改属性.它们在编译时设置,无论如何都保持其值.

与获取属性"相同.您可以使用它来创建一个只读属性(包括变灰的属性名称),但是无法在运行时进行更改.

我会尝试使用几乎类似于您实际类的所有属性的代理类.使代理环绕实际的类,然后将PropertyGrid.SelectedObject设置为您的代理.这样,您可以具有具有不同属性(或getter-setter可用性)的不同代理来更改访问ro/rw.

根据需要启用/禁用多少个属性进行编辑,可能需要更复杂的设计.

不好,但是恕我直言,值得一试.
You cannot change attributes at runtime. They are set at compile time and keep their value no matter what.

Same thing with the get-only-property. You can use this to create a read-only property (including grayed-out property name) but there is no way to change that at runtime.

I''d try using a proxy class that nearly resembles all properties of your actual class. Make the proxy wrap around the actual class and set the PropertyGrid.SelectedObject to your proxy. This way you could have different proxies with different attributes (or getter-setter availability) to change access ro/rw.

Depending on how many properties need to be en-/disabled for editing, an even more complicated desing may be necessary.

Not nice, but IMHO worth a try.


这篇关于带PropertyGrid的类属性的ReadOnly属性的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆