在 PropertyGrid 上显示对象动态类型的属性

发布于 2024-10-20 07:20:06 字数 636 浏览 1 评论 0原文

我需要启用任意对象的编辑属性(对象的类型仅在运行时已知)。我创建了以下类:

public class Camera
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object Configuration
    {
        get
        {
            return configuration; 
        }
        set 
        {
            configuration = value;
        }
    }

    public Class1 a;
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Class1 A
    {
        get
        {
            return a; 
        }
        set 
        {
            a = value;
        }
    }
}

选择对象“Camera”后,我可以在PropertyGrid上看到Class1的属性,但看不到对象“Configuration”的属性。我该如何解决这个问题?

I need to enable editing properties of arbitrary objects (the type of object is only known at run-time). I created the following class:

public class Camera
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object Configuration
    {
        get
        {
            return configuration; 
        }
        set 
        {
            configuration = value;
        }
    }

    public Class1 a;
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Class1 A
    {
        get
        {
            return a; 
        }
        set 
        {
            a = value;
        }
    }
}

After selecting object "Camera", I can see the property of Class1 on PropertyGrid, but I can't see the property of object "Configuration". How can I fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2024-10-27 07:20:06

我的假设是您的表单在分配配置属性之前变得可见。您没有提供足够的代码来查看情况是否如此。为了测试我的担忧,我创建了两个配置对象:

public class Configuration1
{
    public string Test { get; set; }
    public byte Test1 { get; set; }
    public int Test2 { get; set; }
}

并将

public class Configuration2
{
    public char Test3 { get; set; }
    public List<string> Test4 { get; set; }
}

您的相机类修改为如下所示:

public class Camera
{
    public Camera()
    {
        Configuration1 = new Configuration1();
        Configuration2 = new Configuration2();
    }
    private object configuration;

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object Configuration { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Configuration1 Configuration1 { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Configuration2 Configuration2 { get; set; }
}

然后我创建了一个带有 PropertyGrid 和两个 Button 实例的表单。我像这样配置了表单交互:

public partial class Form1 : Form
{
    private readonly Camera camera = new Camera();
    public Form1()
    {
        InitializeComponent();

        propertyGrid1.SelectedObject = camera;
    }

    private void Button1Click(object sender, System.EventArgs e)
    {
        camera.Configuration = new Configuration2();
        UpdatePropertyGrid();
    }

    private void Button2Click(object sender, System.EventArgs e)
    {
        camera.Configuration = new Configuration1();
        UpdatePropertyGrid();
    }

    private void UpdatePropertyGrid()
    {
        propertyGrid1.Refresh();
        propertyGrid1.ExpandAllGridItems();
    }
}

启动视图如下所示:

在此处输入图像描述

单击第一个按钮后:

< img src="https://i.sstatic.net/ZNMfv.jpg" alt="在此处输入图像描述">

单击第二个按钮后:

在此处输入图像描述

如果删除刷新,属性网格将无法正常工作。另一种方法是在类和属性上提供带有 INotifyPropertyChanged 的​​接口。

My assumption was that your form becomes visible before the Configuration property was assigned. You didn't supply enough code to see if that was the case. In order to test out my concern, I created two configuration objects:

public class Configuration1
{
    public string Test { get; set; }
    public byte Test1 { get; set; }
    public int Test2 { get; set; }
}

and

public class Configuration2
{
    public char Test3 { get; set; }
    public List<string> Test4 { get; set; }
}

I modified your camera class to look like this:

public class Camera
{
    public Camera()
    {
        Configuration1 = new Configuration1();
        Configuration2 = new Configuration2();
    }
    private object configuration;

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object Configuration { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Configuration1 Configuration1 { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public Configuration2 Configuration2 { get; set; }
}

I then created a form with a PropertyGrid and two Button instances. I configured the form interactions like this:

public partial class Form1 : Form
{
    private readonly Camera camera = new Camera();
    public Form1()
    {
        InitializeComponent();

        propertyGrid1.SelectedObject = camera;
    }

    private void Button1Click(object sender, System.EventArgs e)
    {
        camera.Configuration = new Configuration2();
        UpdatePropertyGrid();
    }

    private void Button2Click(object sender, System.EventArgs e)
    {
        camera.Configuration = new Configuration1();
        UpdatePropertyGrid();
    }

    private void UpdatePropertyGrid()
    {
        propertyGrid1.Refresh();
        propertyGrid1.ExpandAllGridItems();
    }
}

The startup view looks like this:

enter image description here

After clicking the first button:

enter image description here

After clicking the second button:

enter image description here

If you remove the refresh, the property grid does not work correctly. The alternative is to supply an interface with INotifyPropertyChanged on your classes and properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文