button1은 CustomPanel 하위 목록으로 들어가면서 추가 될 필드를 지정할 수 있습니다.

TableLayout 에서 Row, RowSpan 등의 속성이 추가되는 것이 궁금하여 찾아본 내용 정리입니다.

CustomPanel 하위 목록에 대해서 디자이너에서 보여줄 필드를 생성 가능

 

Panel 외에도 다른 컨트롤을 상속받아서도 가능합니다.

내부적으로 Dictionary 처리를 통해 값을 별도로 관리할 수 있습니다.

ProvideProperty 는 여러 개를 사용할 수 있습니다.

IExtenderProvider 를 상속받아야 하며 Setter와 Getter를 구현해주어야 사용 가능합니다.

아래는 예시 코드입니다.

using System.Collections.Generic;
using System.ComponentModel; //IExtenderProvider
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    //해당 이름 사용. Get, Set 구현 필수
    //CustomField 이기때문에 GetCustomField(Control, 타입), SetCustomField(Control) 메소드 필요.
    [ProvideProperty("CustomField", typeof(Control))]
    class CustomPanel : Panel, IExtenderProvider
    {
        private readonly Dictionary<Control, int> fieldDict = new Dictionary<Control, int>();

        public bool CanExtend(object extendee)
        {
            Control control = extendee as Control;

            if (control == null)
            {
                return false;
            }

            //조건에 따라 전체 컴포넌트에 붙을 수 있음.
            return control.Parent == this; //자식 컴포넌트에만 붙여주기
        }

        [DisplayName("CustomField")] //없으면 클래스명의 필드로 이름 지정됨.
        public int GetCustomField(Control control)
        {
            if (fieldDict.TryGetValue(control, out int value))
            {
                return value;
            }

            return 0; //default value
        }

        public void SetCustomField(Control control, int value)
        {
            fieldDict[control] = value;
        }
    }
}

 

+ Recent posts