UserControl中子FlowLayoutPanel的自定义设计器

问题描述

我创建了一个StackingControl,它基本上是一个UserControl,其中包含一个FlowLayoutPanel(以及一个面板作为页脚)。 StackingControl的目的是容纳从BrickControl派生的一堆相同类型的控件。

我的整个代码如下:

    [Designer(typeof(StackingControlDesigner))]
    public partial class StackingControl : UserControl
    {
        public StackingControl()
        {
            InitializeComponent();
            TypeDescriptor.AddAttributes(this.flowPanel,new DesignerAttribute(typeof(StackingControlContentDesigner)));
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel ContentPanel
        {
            get { return flowPanel; }
        }

        public Type ContainedType
        {
            get
            {
                if (flowPanel.Controls.Count > 0)
                    return flowPanel.Controls[0].GetType();
                return null;
            }
        }
    }




    public class StackingControlContentDesigner : ParentControlDesigner
    {
        private StackingControl parent;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            // first parent is TableLayoutPanel; then StackingControl
            parent = (StackingControl)this.Control?.Parent?.Parent;
        }

        protected override void PostFilterAttributes(IDictionary attributes)
        {
            base.PostFilterAttributes(attributes);
            attributes[typeof(DockingAttribute)] = new DockingAttribute(DockingBehavior.Never);
        }

        protected override void PostFilterProperties(IDictionary properties)
        {
            base.PostFilterProperties(properties);
            var propertiesToRemove = new string[] { "Dock","Anchor","Size","Location","Width","Height","MinimumSize","MaximumSize","AutoSize","AutoSizeMode","Visible","Enabled" };
            foreach (var item in propertiesToRemove)
            {
                if (properties.Contains(item))
                    properties[item] = TypeDescriptor.CreateProperty(this.Component.GetType(),(PropertyDescriptor)properties[item],new BrowsableAttribute(false));
            }
        }

        public override bool CanParent(Control control)
        {
            if (!(control is BrickControl))
                return false;
            if (parent.ContainedType == null)
                return true;
            return parent.ContainedType.Equals(control.GetType());
        }

        public override bool ParticipatesWithSnapLines => false;
    }
    


    public class StackingControlDesigner : ParentControlDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            var contentPanel = ((StackingControl)this.Control).ContentPanel;
            EnableDesignMode(contentPanel,"ContentPanel");
        }

        public override bool CanParent(Control control) => false;
        protected override void OnDragOver(DragEventArgs de) => de.Effect = DragDropEffects.None;
        protected override IComponent[] CreateToolCore(ToolboxItem tool,int x,int y,int width,int height,bool hasLocation,bool hasSize) => null;
    }

问题:当我将BrickControl放到StackingPanel上时,它被添加到FlowLayoutPanel中,但是顺序不明确,我无法对控件进行重新排序就像我通常在FlowLayoutPanel中一样。我发现System.Windows.Forms.Design中有一个FlowLayoutPanelDesigner,但它设置为internal。这是屏幕截图:

Screenshot

我的目标:我打算为FlowLayoutPanel使用自定义设计器来覆盖CanParent(Control)方法,并确保只有一种类型的BrickControl是添加。并提供拖放过程中的一些视觉反馈。

问题:是否有解决方法或类似的方法可以实现我的想法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...