簡體   English   中英

無法在設計器中顯示自定義控件的屬性

[英]Unable to make property of custom control visible in designer

我有以下代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Tools
{
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class ToolStripNumericUpDown : CustomToolStripControlHost
    {
        private NumericUpDown nud;

        public ToolStripNumericUpDown()
            : base(new NumericUpDown())
        {
            this.nud = this.Control as NumericUpDown;


        }

        [Description("The number of decimal places"),
        Category("Custom"),
        EditorBrowsable(EditorBrowsableState.Always),
        Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        Bindable(true),
        DefaultValue(2)]
        public int DecimalPlaces
        {
            get { return nud.DecimalPlaces; }
            set { nud.DecimalPlaces = value; }
        }

        protected override void OnSubscribeControlEvents(Control control)
        {
            base.OnSubscribeControlEvents(control);
            //Add your code here to subscribe Control Events
        }

        protected override void OnUnsubscribeControlEvents(Control control)
        {
            base.OnUnsubscribeControlEvents(control);
            //Add your code here to unsubscribe control events.
        }
    }
}

並在一個單獨的文件中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace System.Windows.Forms
{
    //[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class CustomToolStripControlHost : ToolStripControlHost
    {
        public CustomToolStripControlHost()
            : base(new Control())
        {
        }

        public CustomToolStripControlHost(Control c)
            : base(c)
        {
        }
    }
}

但是,盡管重建了項目,關閉並重新打開了設計器,但DecimalPlaces屬性仍拒絕出現在設計器中。有人可以對此進行說明嗎?

我正在使用VS 2013社區,目標是.NET 4。

謝謝

編輯:為了澄清,我的意思是,當我將我的ToolstripNumericUpDown添加到ContextMenuStrip時,該屬性不會顯示在VS設計器的屬性網格中-實際上,單擊控件完全不會更改屬性網格; 屬性網格仍顯示先前選擇的控件的屬性。

編輯2:情節變厚。 如果選擇控件的父級ToolStripMenuItem,然后訪問該控件的DropDownItems,則可以看到控件,還可以查看和編輯其所有屬性。

Control是泛型類,要顯示小數位,您需要進行類型轉換以將Control轉換為特定類型(NumericUpDown)。

下面的代碼將起作用:

public CustomToolStripControlHost(Control c)
        : base(c)
    {
        NumericUpDown numUpDown = (NumericUpDown)c;
        numUpDown.DecimalPlaces = 10;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM