Как установить CategoryAttribute PropertyGrid в SelectedItem списка - PullRequest
0 голосов
/ 23 ноября 2018

У меня есть проект Windows Forms, в котором я пытаюсь установить атрибуты PropertyGrid со свойствами элемента, выбранного в ListBox.У меня есть ListBox с именем listBox1 с двумя значениями (называемыми «временным» и «временным2») и PropertyGrid с именем propertyGrid1.

Вот мой код:

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'

Это то, что я ожидаю

Если кто-то может указать, как получить PropertyGridустановить имя категории для выбранного элемента списка, это было бы здорово.Спасибо!

...