[英]How to list all instances of a component of typeX on a form in a drop down of a control property in design time
Following situation:如下情况:
I have a component class named ButtonGroupStyleController
and a control class named EnhancedButton
with this property:我有一个名为
ButtonGroupStyleController
的组件 class 和一个名为EnhancedButton
的控件 class,具有以下属性:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(true)]
public ButtonGroupStyleController StyleController { get; set;}
During desing-time in the forms designer, I now want to populate a drop-down in the property grid for this Property with all ButtonGroupStyleController
instances that are currently placed on the form similar to the standard form properties AcceptButton
and CancelButton
, which list all Button instances on the form.在 forms 设计器的设计期间,我现在想使用当前放置在类似于标准表单属性
AcceptButton
和CancelButton
的表单上的所有ButtonGroupStyleController
实例填充此属性的属性网格中的下拉列表,其中列出了所有 Button表格上的实例。
I hope I described my problem clearly and understandable.我希望我能清楚且易于理解地描述我的问题。
The STyleControllerCode is still nearly empty currently because I wanted to implement the function in the question first STyleControllerCode 目前仍然几乎是空的,因为我想先在问题中实现 function
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));
}
}
}
Based on my test, you can define the following class to show the list of instances in design time.根据我的测试,您可以定义以下 class 以在设计时显示实例列表。
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));
}
}
After you added two class, please rebuild the project and add EnhanceButton and ButtonGroupStyleController to your form from the Toolbox.添加两个 class 后,请重建项目并从工具箱中将 EnhanceButton 和 ButtonGroupStyleController 添加到表单中。
Finally, please find the property called stylecontroller and you will see the result.最后,请找到名为 stylecontroller 的属性,您将看到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.