2011-10-19 43 views
3

我设计了一个可以在运行时扩展或折叠窗体的自定义面板。 enter image description here为什么自定义控制任务窗格不更新其属性?

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

我的控制类的代码:

taskpanedesign类的
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; 
      } 
     } 

代码:

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

+0

从任务窗格中的值150正常工作的第一次,但不能更新改变后。 –

+0

所做的只是改变一个字段('usrVerticalSize'),这似乎并没有在其他地方使用(除非你没有显示) - 那么该值如何被使用?什么绑定到'SetVerticalSize'?特别是,它从来没有以任何方式实际改变控件的大小。 –

回答

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; 
    } 
相关问题