【问题标题】:How to list all instances of a component of typeX on a form in a drop down of a control property in design time如何在设计时在控件属性的下拉列表中列出表单上 typeX 组件的所有实例
【发布时间】:2021-04-19 07:27:48
【问题描述】:

以下情况:

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

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

在表单设计器的设计期间,我现在想在此属性的属性网格中填充一个下拉列表,其中包含当前放置在表单上的所有 ButtonGroupStyleController 实例,类似于标准表单属性 AcceptButtonCancelButton,它列出了表单上的所有 Button 实例。

我希望我清楚地描述了我的问题并且可以理解。

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));
        }
    }
}

【问题讨论】:

  • 签出this
  • @user5687083,你能提供关于ButtonGroupStyleController类的代码吗?
  • @user5687083,根据我的进一步研究,我得到了图片 [1]:i.stack.imgur.com/I9mwy.png。是你想要的吗?如果没有,请告诉我你想列出什么?
  • 杰克,这绝对是我想要达到的目标。我用谷歌搜索了很多地方,但我从来没有找到正确的东西。

标签: c# forms visual-studio propertyeditor form-designer


【解决方案1】:

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

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,如果是这样,您可以单击“✔”将相应的回复标记为答案。它还将帮助其他人解决类似的问题。
猜你喜欢
  • 1970-01-01
  • 2015-05-14
  • 2011-02-15
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多