如何解决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。这是屏幕截图:
我的目标:我打算为FlowLayoutPanel
使用自定义设计器来覆盖CanParent(Control)
方法,并确保只有一种类型的BrickControl
是添加。并提供拖放过程中的一些视觉反馈。
问题:是否有解决方法或类似的方法可以实现我的想法?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。