winforms UserControl中嵌套控件的自定义设计器中的EnableDesignMode不起作用

bvjveswy  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(108)

我正在通过UserControl设计一个自定义向导控件。我使用的是一个名为WizardPageControl的子控件,它是从TabControl派生的,用于托管向导页面,并需要在最终的Wizard用户控件的设计视图中进行编辑。我通过下面的代码实现了这一点:

[Designer(typeof(WizardDesigner))]
public partial class Wizard : BaseUserControl
{
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public WizardPageControl WizardPageControlArea
    {
        get { return wizardPageControl; }
    }

    public Wizard()
    {
        InitializeComponent();
    }
}

internal class WizardDesigner : ParentControlDesigner
{

    public override void Initialize(System.ComponentModel.IComponent component)
    {
        base.Initialize(component);
        var uc = (Wizard)component;
        EnableDesignMode(uc.WizardPageControlArea, "WizardPageControlArea");            
    }
}

如果我构造一个简单的Wizard UserControl,其中一个Label停靠在顶部,另一个Label停靠在底部,一个名为wizardPageControlWizardPageControl设置为Dock Fill,当我构建并将控件放置在窗体上时,我会得到以下结果:

一切都很好。除了当我关闭并重新打开表单时,我得到了这个(移动手柄和轮廓框是wizardPageControl):

基本上,wizardPageControl Dock-Fill已经扩展到填充整个表单,而不是在顶部和底部标签停靠点之间。很简单放在一个面板里对吧没有骰子。只要我把它放在任何类型的容器中,VS在试图将Wizard UserControl添加到任何表单时都会崩溃。我猜这是因为wizardPageControl(及其对应的公共属性WizardPageControlArea)不再是窗体的直接子控件,而是另一个容器的子控件。
我已经阅读了EnableDesignModeINestedContainer的MS文档,但仍然发现它像台风中的雨伞一样有用。
任何帮助都将受到感激。🙂🙏

omtl5h9j

omtl5h9j1#

所以我从来没有通过在面板中嵌套可编辑控件并对接它来解决这个问题,而是通过不同的方法来解决这个问题。我在User Control OnLoad事件中手动定位控件,并通过可编辑控件上的锚属性处理UC大小调整。在我的示例中,我将其定位在顶部页眉的正下方,并将锚设置为左、上、右和下(这样它就可以与控件一起拉伸)。
但是,您还需要防止用户将控件拖动到UC的不可编辑区域,并且还必须阻止用户调整可编辑控件的大小或移动可编辑控件。这是通过下面的代码实现的:

[Designer(typeof(WizardDesigner))]
public partial class Wizard : BaseUserControl
{
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public WizardPageControl WizardPageControlArea
    {
        get { return wizardPageControl; }
    }

    public Wizard()
    {
        InitializeComponent();
    }
    
    private void Wizard_Load(object sender, EventArgs e)
    {
        wizardPageControl.Location = new Point(0, TopPN.Height);
        wizardPageControl.Size = new Size(this.Width, this.Height - TopPN.Height - BottomPN.Height);
    }   
}

internal class WizardDesigner : ParentControlDesigner
{

    public override void Initialize(System.ComponentModel.IComponent component)
    {
        base.Initialize(component);
        var uc = (Wizard)component;
        EnableDesignMode(uc.WizardPageControlArea, "WizardPageControlArea");            
    }
    
    // prevents dropping of controls onto non-Design-mode enabled areas
    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)
    {
        return null;
    }   
}

[Designer(typeof(Designers.WizardPageControlDesigner))]
public class WizardPageControl: UIElements.TabControl
{
    public WizardPageControl()
    {
    }
}

internal class WizardPageControlDesigner : ParentControlDesigner
{
    // Prevents user from resizing or moving the editable control
    public override SelectionRules SelectionRules
    {
        get
        {
            SelectionRules selectionRules = base.SelectionRules;
            selectionRules &= ~SelectionRules.AllSizeable;
            selectionRules &= ~SelectionRules.Moveable;
            return selectionRules;
        }
    }
    
    // Hides other properties from user which may chnage the positoning of the control
    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","DrawMode","HideBorders","HideTabs"
            };
        foreach (var item in propertiesToRemove)
        {
            if (properties.Contains(item))
                properties[item] = TypeDescriptor.CreateProperty(this.Component.GetType(),
                    (PropertyDescriptor)properties[item],
                    new BrowsableAttribute(false));
        }
    }
}

希望它能拯救我生命中失去的两天!🙂

相关问题