C# 为什么自定义控件任务窗格不更新其属性?

C# 为什么自定义控件任务窗格不更新其属性?,c#,custom-controls,C#,Custom Controls,我设计了一个自定义面板,可以在运行时展开或折叠窗体。 当我从自定义设计任务更改其高度时,它不会更新它。 我的控制类代码: using System; using System.Windows.Forms; using System.ComponentModel; using System.ComponentModel.Design; using System.Windows.Forms.Design; [Designer(typeof(MyControlDesigner))] publi

我设计了一个自定义面板,可以在运行时展开或折叠窗体。

当我从自定义设计任务更改其高度时,它不会更新它。

我的控制类代码:

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;

[Designer(typeof(MyControlDesigner))]
 public partial class ExpandCollapsePanel : UserControl
    {    
        private bool flag = false;
        private Size size;
        public int usrVerticalSize;

        public ExpandCollapsePanel()
        {
            InitializeComponent();

        }       
        [DefaultValueAttribute(true)]
        public int SetVerticalSize
        {
            get
            {
                return usrVerticalSize;
            }
            set
            {
                usrVerticalSize = value;
            }
        }
taskpanedesign类的代码:

namespace ExpandCollapseFormLibrary
{
    class CustomDialogue : ControlDesigner
    {
        private DesignerActionListCollection actionLists;
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (actionLists == null)
                {
                    actionLists = new DesignerActionListCollection();
                    actionLists.Add(new MyActionListItem(this));
                }
                return actionLists;
            }
        }
    }
    internal class MyActionListItem : DesignerActionList
    {
        public MyActionListItem(ControlDesigner owner) : base(owner.Component)
        {
        }
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            var items = new DesignerActionItemCollection();
            //items.Add(new DesignerActionTextItem("Hello world", "Misc"));
            items.Add(new DesignerActionPropertyItem("Checked", "Vertical Drop Down Size"));
            return items;
        }

        public int Checked
        {
            get { return ((ExpandCollapsePanel)base.Component).SetVerticalSize; }
            set { ((ExpandCollapsePanel)base.Component).SetVerticalSize = value; }
        }

    }    
}
当我更改该值时,Form1(拖放的位置)设计的类将永久保留该值。

自定义窗格的SetVerticalSize属性值确实发生了更改,但问题是设计器宿主根本不知道这一点。要通知设计器宿主您的自定义窗格更改,您应该实现如下内容(我建议您阅读以了解更多详细信息):


“任务窗格”中的值为150,首次正常工作,但更改后无法更新。所做的只是更改一个字段(
usrVerticalSize
),该字段似乎在其他任何地方都没有使用(除非您没有显示该字段)-该值随后如何使用?什么绑定到
SetVerticalSize
?特别是,它实际上从未以任何方式更改控件的大小。
    int usrVerticalSize;
    [DefaultValue(true)]
    public int SetVerticalSize {
        get { return usrVerticalSize; }
        set {
            FireChanging(); //changing notification 
            try {
                usrVerticalSize = value;
            }
            finally { FireChanged(); } //changed notification 
        }
    }
    void FireChanging() {
        IComponentChangeService service = GetComponentChangeService();
        if(service != null)
            service.OnComponentChanging(this, null);
    }
    void FireChanged() {
        IComponentChangeService service = GetComponentChangeService();
        if(service != null)
            service.OnComponentChanged(this, null, null, null);
    }
    IComponentChangeService GetComponentChangeService() {
        return GetService(typeof(IComponentChangeService)) as IComponentChangeService;
    }