为什么自定义控件任务窗格不更新其属性? [英] Why custom control task pane does not update its properties?

查看:162
本文介绍了为什么自定义控件任务窗格不更新其属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设计了一个自定义面板,可以扩展或在运行时崩溃的形式。

I have designed a custom panel which can expand or collapse form at run time.

当我从定制设计的任务,改变它的高度,它不会更新。

When I change its height from custom designed task, it does not update it.

代码我的控制类:

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类代码:

Code of taskpanedesign class:

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(其中拖动和删除)设计类永远保存。

When I change the value the Form1(where drag and dropped) designed class keep it permanently.

推荐答案

自定义面板的的SetVerticalSize属性值是真的变了,但问题是,设计师主机不知道它在所有。通知设计师主机您的自定义面板改变了你应该实现这样的事情(我建议你读的 IComponentChangeService MSDN文章了解更多详情):

the SetVerticalSize property value of your custom pane's is really changed, but the problem is that the designer host does not know about it at all. To notify the designer host about your custom pane changing you should implement something like this (I suggest you read the IComponentChangeService MSDN article for more details):

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

这篇关于为什么自定义控件任务窗格不更新其属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆