C# 如何在设计时在控件属性下拉列表中列出窗体上typeX组件的所有实例

C# 如何在设计时在控件属性下拉列表中列出窗体上typeX组件的所有实例,c#,forms,visual-studio,propertyeditor,form-designer,C#,Forms,Visual Studio,Propertyeditor,Form Designer,以下情况: 我有一个名为ButtonGroupStyleController的组件类和一个名为EnhancedButton的控件类,具有以下属性: [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Browsable(true)] public ButtonGroupStyleController StyleController { get; set;} 在表单设计器的设计期间,我现在想在属性

以下情况:

我有一个名为
ButtonGroupStyleController
的组件类和一个名为
EnhancedButton
的控件类,具有以下属性:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true)]
public ButtonGroupStyleController StyleController { get; set;}
在表单设计器的设计期间,我现在想在属性网格中为该属性填充一个下拉列表,其中包含当前放置在表单上的所有
ButtonGroupStyleController
实例,这些实例类似于标准表单属性
AcceptButton
CancelButton
,其中列出了表单上的所有按钮实例

我希望我能清楚明白地描述我的问题

STyleControllerCode目前仍然几乎为空,因为我想首先实现问题中的函数

namespace DarkTower.Core.Rendering.Forms
{
    [Serializable]
    public class ButtonGroupStyleController : Component, INotifyPropertyChanged
    {
        public ButtonGroupStyleController()
        {

        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


根据我的测试,您可以定义以下类来显示设计时的实例列表

public class EnhancedButton:Button
    {

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Browsable(true)]
        public ButtonGroupStyleController StyleController { get; set; }

    }

 [Serializable]
    public class ButtonGroupStyleController : Component, INotifyPropertyChanged
    {

        public ButtonGroupStyleController()
        {
            this.Text = "Hi Winform";
        }
        private string text;

        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                OnPropertyChanged("Text");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        //[NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName=null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            
        }

    }
添加两个类后,请重新生成项目,并从工具箱中将EnhanceButton和ButtonGroupStyleController添加到表单中


最后,请找到名为stylecontroller的属性,您将看到结果。

签出。@user5687083,您能提供关于ButtonGroupStyleController类的代码吗?@user5687083,根据我的进一步研究,我得到了pic[1]:。是你想要的吗?如果没有,请告诉我你想列出什么?杰克,这绝对是我想要实现的。我在谷歌上搜索了很多地方,但我从来没有找到正确的东西。杰克,很好用,谢谢lot@user5687083,如果是,您可以单击'✔' 将适当的回答标记为答案。它还将帮助其他人解决类似问题。