How to force a specific UserControl design

I am writing a basic UserControl that will be inherited by a bunch of other UserControls. I need to provide a specific design for all these descendant controls (for example, a few buttons should be on top, along with a label or two).

The rest of the descendant UserControl area can freely have everything on it.

Initially, I thought that I could just flip the panel to Base UserControl, set Dock = Fill, and the descendant control developer would be forced to add the entire user interface to that panel. Then I can resize the panel to my content.

But this is not the case - when you drop a control (say, GridView) on a child of UserControl, it adds it to the .Controls collection of the user control of the child, not Panel I.

Is there a way to force a specific layout from a Base user control?

+3
source share
2 answers

The short answer is β€œYes” ... However, in order for this behavior to penetrate the ugly world of writing your own designers, which you need to associate with each control that needs to inherit a special placement in the content panel ..

, , , .

http://support.microsoft.com/?id=813808

, , . (30+ ) , , . , .

, , , , ? , , , ?

+4

AngryHacker -

, , , , WinForm. , VB6:-).

, EnableDesignMode(). WinForm. , ParentControlDesigner, UserControl . ButtonBarDesigner Initialize(), , ButtonBar strong > fillPanel, "FillPanel".

, , . ButtonBar, . , , , , ButtonBar. , , EnableDesignMode() . DesignerSerializationVisibility FillPanel. .

System.Design .

, ButtonBar:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Threading;

namespace ForceUserControl
{
    [Designer(typeof(ButtonBarDesigner))]
    public partial class ButtonBar : UserControl
    {
        public ButtonBar()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Returns inner panel.
        /// </summary>
        /// <remarks>Should allow persistence.</remarks>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel FillPanel
        {
            get { return fillPanel; }
        }

    }

    private class ButtonBarDesigner : ParentControlDesigner
    {
        public override void Initialize(IComponent component)
        {
        base.Initialize(component);

            Panel fillPanel = ((ButtonBar)component).FillPanel;

            // The name should be the same as the public property used to return the inner panel control.
            base.EnableDesignMode(fillPanel, "FillPanel");
        }
    }
}
+4

Source: https://habr.com/ru/post/1723524/


All Articles