创建界面和用户控件
public interface IMyUserControl
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[Browsable(false)]
[ReadOnly(true)]
object MyProp { get; set; }
}
public partial class MyUserControl : UserControl, IMyUserControl
{
public MyUserControl()
{
InitializeComponent();
}
public object MyProp { get; set; }
}
然后构建项目并将MyUserControl从Toolbox中删除到Form1。 请参见Form1.Designer.cs
private void InitializeComponent()
{
// other code
this.myUserControl1.MyProp = null;
// other code
}
但下面的代码工作正常(设计师不会生成this.myUserControl1.MyProp = null)
public partial class MyUserControl : UserControl, IMyUserControl
{
public MyUserControl()
{
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public object MyProp { get; set; }
}