c# - 为什么自定义控件任务 Pane 不更新其属性?

标签 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

最佳答案

您的自定义 Pane 的 SetVerticalSize 属性值确实发生了变化,但问题是设计器宿主根本不知道它。要通知设计器主机您的自定义 Pane 更改,您应该执行类似这样的操作(我建议您阅读 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;
    }

关于c# - 为什么自定义控件任务 Pane 不更新其属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818284/

相关文章:

javascript - 如何在WebMethod中接收非原始数据?

wpf - 模板可以绑定(bind)到几何图形吗

c# - 仅在 .appxupload 中缺少系统程序集

c# - 如果关联的 SqlConnection 将被处置,是否需要 SqlCommand.Dispose()?

c# - asp CompareValidator 在回发后验证

.net - 更改 Canvas 内控件的位置后,滚动条不可见

c# - 访问其他 Twitter 用户信息

c# - 当我需要多个相同类型的关系时 Entity Framework 出现问题

c# - 优化自定义控件的javascript

iPhone UI 库?