如何在设计时公开复合控件内的子控件

 

使用UserControl制作一个复合控件,有时内部一个子控件很多属性、方法和事件都要和外部交互,如果要在UserControl重新公开这些接口将是一个工作量很大的事情,我们可以考虑直接向外公开这个子控件,就是在UserControl提供一个公有的属性可以直接访问到这个子控件,如果我们要做到设计时的控制,(就像Panel内的控件可以直接拖放的效果一样),我们就得使用自己的设计器Designer。通过Designer定制属性加载自己的设计器。

如:

ContractedBlock.gif ExpandedBlockStart.gif Code
[Designer(typeof(LawCaseDesigner))]
[DesignTimeVisible(
true)]
[DefaultEvent(
"SearchLawCase")]
public partial class CtrlLawCaseSearch : UserControl
{
        
/// <summary>
        
/// 搜索按键
        
/// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        
public XPButton BtnSearch
        {
            
get { return this.btnSearch; }
        }
……

 
     如果单单想公开一个或多个子控件,自己的设计器可以从ControlDesigner派生;在公司子控件的同时又想把这个UserControl作为一个容器,可以从ParentControlDesigner派生。

通过重写Initialize()方法,使用EnableDesignMode方法公开子控件。

示例:

ContractedBlock.gif ExpandedBlockStart.gif LawCaseDesigner
   internal class LawCaseDesigner : ParentControlDesigner
    {
        
private CtrlLawCaseSearch MyControl;
        
public override void Initialize(IComponent component)
        {
            
base.Initialize(component);
           
// Record instance of control we're designing
            MyControl 
= (CtrlLawCaseSearch)component;
            
this.EnableDesignMode(MyControl.BtnSearch, "btnSearch");
            
//this.EnableDesignMode(MyControl.BtnTest, "BtnTest");
        }
    }

   

转载于:https://www.cnblogs.com/Yjianyong/archive/2009/08/06/1540676.html

猜你喜欢

转载自blog.csdn.net/weixin_33775572/article/details/94199500
今日推荐