C#包含子项的用户控件

时间:2013-10-06 13:28:40

标签: c# user-controls children

我创建了一个继承自Panel的用户控件。目标是拥有一组可折叠面板。

Here is how my user control looks like (no comment on ugly it's :P) 我正在使用控件的主类,另一个用于集合(使用CollectionBase)和第三个用于控件。

我的问题是当我添加一个项目时,我需要主控件来添加另一个面板。目前绘图看起来很好,但我没有得到预期的结果。

我不确定我是否清楚地暴露了我的问题。假设您在Visual Studio中将Panel1放在Form1上,将Panel2 + Panel 3放在Panel 1中

Form1.Designer.cs将包含:

    Form1.Controls.Add(Panel1);
    Panel1.Controls.Add(Panel2); 
    Panel1.Controls.Add(Panel3);

这是我愿意做的。我试图创建一个事件OnItemAdded并订阅它以便添加面板但它不像魅力一样工作,而且它在任何编译时都是触发器,每次集合都需要填充时只完成一次。

我很抱歉我的英语不好,如果我需要澄清一些事情,请不要犹豫告诉我。我不确定代码的某些部分是否需要,但如果你愿意,可以问一下!

提前感谢您的提示!

嗨terrybozzio,谢谢你回答。

以下是代码的重要部分。 头等舱,主要

    [Designer(typeof(CollapsiblePanelDesigner))]
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))] 
    public partial class CollapsiblePanel : Panel
    {
        private CollapsiblePanelItemCollection _items = null;

        [Browsable(true)]
        [Editor(typeof(CollapsiblePanelItemCollectionEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

        public CollapsiblePanelItemCollection Items
        {
            get { return _items; }
        }

        public CollapsiblePanel()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint ControlStyles.OptimizedDoubleBuffer, true);
            _items = new CollapsiblePanelItemCollection(this);
        }

}

继承CollectionBase的第二个类如下:

    public class CollapsiblePanelItemCollection : CollectionBase
    {
        private CollapsiblePanel _owner = null;

        public int IndexOf(CollapsiblePanelItem item)
        {
            return InnerList.IndexOf(item);
        }

        public CollapsiblePanelItem this[int index]
        {
            get { return (CollapsiblePanelItem)InnerList[index]; }
        }

        public CollapsiblePanelItemCollection(CollapsiblePanel owner)
        {
            _owner = owner;
        }

        public void Add(CollapsiblePanelItem item) { }

        public bool Remove(CollapsiblePanelItem item) { }

}

    public class CollapsiblePanelItemCollectionEditor : CollectionEditor
    {

        private Type[] _types;

        public CollapsiblePanelItemCollectionEditor(Type type) : base(type)
        {
            _types = new Type[] { typeof(CollapsiblePanelItem) };
        }

        protected override Type[] CreateNewItemTypes()
        {
            return _types;
        }
    }

第三类,集合的项目:

    [DefaultProperty("Text")]
    [TypeConverter(typeof(CollapsiblePanelItemConverter))]
    public class CollapsiblePanelItem
    {
        private bool _isCollapsed = false;
        private string _text = "Text"; 
        private int _index = -1;
        private Panel _panel = new Panel();

        // simplified properties of course
        [DefaultValue(false)]
        public bool IsCollapsed { { get ; set; } }

        [DefaultValue(typeof(string), "Text")]
        public string Text { { get ; set; } }

        [Browsable(false)]
        public int Index { { get ; set; } }

        public Panel Panel { { get ; set; } }

        public CollapsiblePanelItem() { }

        public CollapsiblePanelItem(string Text, int ContainerSize, Image Image, bool IsCollapsed)
        {
            _text = Text;
            _containerHeight = ContainerHeight;
            _headerImage = Image;
            _isCollapsed = IsCollapsed;
        }
    }

    public class CollapsiblePanelItemConverter : ExpandableObjectConverter
    {

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (destinationType == typeof(InstanceDescriptor)) ? true : base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                if (value is CollapsiblePanelItem)
                {
                    CollapsiblePanelItem objValue = (CollapsiblePanelItem)value;
                    Type[] types = new Type[3];
                    object[] values = new object[3];
                    types[0] = typeof(Panel);
                    values[0] = objValue.Panel;
                    types[1] = typeof(string);
                    values[1] = objValue.Text;
                    types[2] = typeof(int);
                    values[2] = objValue.Index;
                    return new InstanceDescriptor(typeof(CollapsiblePanelItem).GetConstructor(types), values, true);
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

当然,当你读到CollapsiblePanelItem类时,你会明白我不知道我在使用Panel做什么......;)

0 个答案:

没有答案
相关问题