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

时间:2011-10-19 08:06:51

标签: c# custom-controls

我设计了一个自定义面板,可以在运行时展开或折叠表单。 enter image description here

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

我的控制类代码:

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(拖放)设计类会永久保留它。 enter image description here

1 个答案:

答案 0 :(得分:3)

自定义窗格的SetVerticalSize属性值确实已更改,但问题是设计器主机根本不知道它。要通知设计人员主机有关更改自定义窗格的信息,您应该实现类似的内容(建议您阅读IComponentChangeService MSDN article了解更多详情):

    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;
    }