You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms自定义CollectionEditor设计时不生效,默认编辑器启动问题

WinForms自定义CollectionEditor设计时不生效问题

问题描述

我正在开发WinForms的C#应用,为自定义控件MainTabControl创建了自定义CollectionEditor(MainTabPageCollectionEditor)。它在运行时配合PropertyGrid可正常工作,但设计时启动的却是默认的CollectionEditor。我希望在设计时能将自定义的MainTabPage(继承自TabPage)添加到MainTabControl(继承自TabControl)中,因此定制了CollectionEditor,但目前启动的是常规TabControl编辑器,而非自定义版本。

相关代码

MainTabControl

public partial class MainTabControl : TabControl
{
    private readonly int mouseAreaTriggerOffset = 3;
    private int standardOffset;
    private Size closeButtonSize;
    private Size textSize;

    [EditorAttribute(typeof(MainTabPageCollectionEditor), typeof(UITypeEditor))]
    [Browsable(true)]
    public new TabPageCollection TabPages
    {
        get
        {
            return base.TabPages;
        }
    }

    public MainTabControl()
    {
        InitializeComponent();

        this.DrawMode = TabDrawMode.OwnerDrawFixed;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        standardOffset = (int)(this.ItemSize.Width * 0.05);

        for (int i = 0; i < this.TabPages.Count; i++)
        {
            // Image
            //Image image = this.ImageList.Images["AddAccount.png"];
            Size imageSize = new Size(e.Bounds.Height / 2, e.Bounds.Height / 2);
            //e.Graphics.DrawImage(image, new Rectangle(e.Bounds.Left + standardOffset, e.Bounds.Top + (e.Bounds.Height - imageSize.Height) / 2, imageSize.Width, imageSize.Height));

            // Text
            textSize = TextRenderer.MeasureText(this.TabPages[e.Index].Text, e.Font);
            e.Graphics.DrawString(this.TabPages[e.Index].Text, e.Font, Brushes.Black,
                e.Bounds.Left + 2 * standardOffset + imageSize.Width,
                e.Bounds.Top + (e.Bounds.Height - textSize.Height) / 2);

            // Close button
            closeButtonSize = TextRenderer.MeasureText("x", e.Font);
            e.Graphics.DrawString("x", e.Font, Brushes.Red,
                e.Bounds.Right - closeButtonSize.Width / 2 - standardOffset,
                e.Bounds.Top + (e.Bounds.Height - closeButtonSize.Height) / 2);

            // Line
            Pen pen = new Pen(Brushes.MediumBlue, 3);
            e.Graphics.DrawLine(pen, e.Bounds.Left + standardOffset, 4, e.Bounds.Right - standardOffset, 4);
        }

        base.OnDrawItem(e);
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        for (int i = 0; i < this.TabPages.Count; i++)
        {
            // ...
        }

        base.OnMouseDown(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        for (int i = 0; i < this.TabPages.Count; i++)
        {
            Rectangle bounds = this.GetTabRect(i);
            Rectangle closureBounds = new Rectangle(
                bounds.Right - closeButtonSize.Width / 2 - standardOffset - mouseAreaTriggerOffset,
                bounds.Top + (bounds.Height - closeButtonSize.Height) / 2 - mouseAreaTriggerOffset,
                closeButtonSize.Width + 2 * mouseAreaTriggerOffset,
                closeButtonSize.Height + 2 * mouseAreaTriggerOffset);

            if (closureBounds.Contains(e.Location))
            {
                // ...
            }
        }

        base.OnMouseMove(e);
    }
}

MainTabPage

public partial class MainTabPage : TabPage
{
    private bool isClosable;

    [Browsable(true)]
    [Description("Indicates whether the tab could be closed")]
    [Category("Behavior")]
    public bool IsClosable { get; set; }

    [Browsable(true)]
    [Description("Indicates whether the tab has the icon")]
    [Category("Appearance")]
    public bool IsIcon { get; set; }

    [Browsable(true)]
    [Description("Indicates whether the tab has upper line")]
    [Category("Appearance")]
    public bool IsUpperLine { get; set; }

    public MainTabPage()
    {
        InitializeComponent();
    }

    public MainTabPage(string text) : this()
    {
        Text = text;
    }
}

MainTabPageCollectionEditor

public class MainTabPageCollectionEditor : CollectionEditor
{
    public MainTabPageCollectionEditor(Type type)
        : base(type)
    {  }

    protected override object CreateInstance(Type itemType)
    {
        object instance = base.CreateInstance(itemType);

        MainTabPage mainTabPage = instance as MainTabPage;
        mainTabPage.UseVisualStyleBackColor = true;

        return mainTabPage;
    }

    protected override Type CreateCollectionItemType()
    {
        return typeof(MainTabPage);
    }
}

界面对比

  • 运行时:显示自定义的MainTabPageCollectionEditor,可直接添加MainTabPage类型的选项卡
  • 设计时:启动默认的CollectionEditor,仅能添加常规TabPage

内容的提问来源于stack exchange,提问作者NeverDoomedEnough

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 15:08:10