如何将PropertyGrid的CategoryAttribute设置为列表的SelectedItem

时间:2018-11-23 07:41:51

标签: c# winforms listbox propertygrid

我有一个Windows Forms项目,我试图在其中设置PropertyGrid的属性以及从ListBox中选择的项目的属性。我有一个名为listBox1的ListBox,其中有两个值(分别称为“ temporary”和“ temporary2”),以及一个名为PropertyGrid1的PropertyGrid。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Diagnostics;

namespace SortListProperties
{
        public partial class MainForm : Form
        {   
                public class SortProperties
                {
                        public string Name;
                        [CategoryAttribute(listBox1.SelectedItem.ToString()), DescriptionAttribute("Description of item goes here")]
                        public string Info
                        {
                                get
                                {
                                        return Name;
                                }

                                set
                                {
                                        Name = value;
                                }
                        }
                }
                public MainForm()
                {
                        InitializeComponent();
                        var properties = new SortProperties();
                        propertyGrid1.SelectedObject = properties;
                }
        }
}

这是初始化代码:

    namespace SortListProperties
    {
        partial class MainForm
        {
                private System.ComponentModel.IContainer components = null;
                public System.Windows.Forms.ListBox listBox1;
                private System.Windows.Forms.PropertyGrid propertyGrid1;

                protected override void Dispose(bool disposing)
                {
                        if (disposing) {
                                if (components != null) {
                                        components.Dispose();
                                }
                        }
                        base.Dispose(disposing);
                }

                private void InitializeComponent()
                {
                        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
                        this.listBox1 = new System.Windows.Forms.ListBox();
                        this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
                        this.SuspendLayout();

                        this.listBox1.FormattingEnabled = true;
                        this.listBox1.Items.AddRange(new object[] {"temporary", "temporary2"});
                        this.listBox1.Location = new System.Drawing.Point(12, 50);
                        this.listBox1.Name = "listBox1";
                        this.listBox1.Size = new System.Drawing.Size(146, 238);
                        this.listBox1.TabIndex = 2;

                        this.propertyGrid1.Location = new System.Drawing.Point(164, 24);
                        this.propertyGrid1.Name = "propertyGrid1";
                        this.propertyGrid1.Size = new System.Drawing.Size(272, 261);
                        this.propertyGrid1.TabIndex = 3;

                        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                        this.ClientSize = new System.Drawing.Size(448, 337);
                        this.Controls.Add(this.propertyGrid1);
                        this.Controls.Add(this.listBox1);
                        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                        this.Name = "MainForm";
                        this.Text = "Get Item Properties Test";
                        this.ResumeLayout(false);
                        this.PerformLayout();

                }
        }
}

运行程序时,出现此错误:

  

(15,23):错误(CS0120):非静态字段,方法或属性'SortListProperties.MainForm.listBox1'需要对象引用

This is what I expect

如果有人可以指出如何获取PropertyGrid来将类别名称设置为列表中的所选项目,那将很棒。谢谢!

0 个答案:

没有答案