我正在制作一些控件,它们都必须具有相同的外观和一些共同的行为,尽管它们用于不同类型的输入.所以我做了一个继承自 UserControl 的 BaseClass,我的所有控件都继承自 BaseClass.
I'm making some controls which all have to share the same look and some common behavior, although they are meant for different kind of inputs. So I made a BaseClass which inherit from UserControl, and all my controls inherit from BaseClass.
但是,如果我在设计器中为 BaseClass 添加控件,例如 TableLayoutPanel,我在设计继承的类时将无法访问它们.我看到了 TableLayoutPanel,但即使他受到保护",我也无法通过设计器修改它或将控件放入其中.我可以通过代码访问它,但我不想失去使用设计器的能力.
However, if i add controls for BaseClass in the designer, such as a TableLayoutPanel, i can't access them when I'm designing the inherited classes. I see the TableLayoutPanel, but even though he is "protected", i can't modify it or put controls in it through the designer. I've no trouble accesing it by code, but i don't want to lose the ability to use the designer.
现在,我只是从 BaseClass 中删除了所有控件,在每个继承的类中添加了布局和所有公共控件,然后使用引用在 BaseClass 中操作它们.但这根本不能让我满意.有没有办法让设计器使用继承的受保护成员控件?
Right now, i simply removed all controls from BaseClass, added the layout and all the common controls in each of the inherited class, then use references to manipulate them inside BaseClass. But that doesn't satisfy me at all. Is there a way to make the designer work with inherited protected member controls ?
环境:C#、.NET 3.5、Visual Studio 2008
Environment : C#, .NET 3.5, Visual Studio 2008
编辑以回答 SLaks 的建议.我尝试设置一个属性,虽然我不习惯使用它们,但它似乎不起作用.这是我试过的代码:
EDIT to answer SLaks's suggestion. I tried setting a property, and although I'm not used to use them it doesn't seem to work. Here is the code i tried :
public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public TableLayoutPanel TableLayoutPanel1 { get { return tableLayoutPanel1;} set { tableLayoutPanel1 = value;} } } public partial class UserControl2 : UserControl1 { public UserControl2() { InitializeComponent(); } } 推荐答案当您尝试从带有设计器的继承控件访问基本控件中声明的 TableLayoutPanel 时,您正在使用 WinForms 中称为视觉继承"的功能".
When you try to access from the inherited control with the designer to the TableLayoutPanel declared in the base control, you're using a feature in WinForms called "Visual Inheritance".
遗憾的是 TableLayoutPanel 不支持视觉继承:msdn.microsoft/en-us/library/ms171689%28VS.80%29.aspx
Unfortunately TableLayoutPanel doesn't support visual inheritance: msdn.microsoft/en-us/library/ms171689%28VS.80%29.aspx
这就是 TableLayoutPanel 在继承的控件中显示为阻塞的原因.
That's why the TableLayoutPanel appears blocked in the inherited controls.
更多推荐
如何在winforms设计器中访问继承的控件
发布评论