技术开发 频道

asp.net控件设计时支持(3)

【IT168 技术文档】

   示例代码

一.智能标记

先看一张图.



GridView右侧的小三角可以很轻松的帮助我们设置常用的属性,如下面的启动分页,启用排序等,通过这样的方式我们可以很快的完成工作。我们称这样的任务菜单为智能标记.

下面来看看如何实现

1.重写ControlDesigner的ActionLists属性

你必须重写这个属性,返回你自定义的智能标记集合(即DesignerActionListCollection),这里假设CustomControlActionList为自定义的智能

    public class SampleControlDesigner : ControlDesigner
    
{
        
public SampleControlDesigner()
            : 
base()
        
{
        }


        
//创建一个自定义操作列表集合
        public override DesignerActionListCollection ActionLists
        
{
            
get
            
{
                DesignerActionListCollection actionLists 
= new DesignerActionListCollection();
                actionLists.Add(
new CustomControlActionList(this));

                
return actionLists;
            }

        }
  
    }



2.CustomControlActionList 自定义项列表

2.1项列表分类

(1)标题面板
(2)属性面板
(3)方法面板

类图如下



看个效果图,你就明白怎么回事了



2.2实现

(1)
继承DesignerActionList类,重写GetSortedActionItems方法添加自定义项面板集合,即2.1的三种项面板

        public override DesignerActionItemCollection GetSortedActionItems()
        
{
            
if (items == null)
            
{
                items 
= new DesignerActionItemCollection();
                
// 添加标题面板
                items.Add(new DesignerActionHeaderItem("快速设置面板测试:"));
                
//添加属性相关面板
                items.Add(new DesignerActionPropertyItem("Visible",
                         
"是否显示"));
                items.Add(
new DesignerActionPropertyItem("Width",
                        
"设置宽度"));
                items.Add(
new DesignerActionPropertyItem("Height",
                       
"设置高度"));
                
// 添加方法相关面板

                items.Add(
new DesignerActionMethodItem(this"FormatBlue""定义背景为蓝色"true));
                items.Add(
new DesignerActionMethodItem(this"FormatRed""定义背景为红色"true));
                items.Add(
new DesignerActionMethodItem(this"FormatWhite""定义背景为白色"true));
                
            }

            
return items;
        }


(2)属性,方法项面板的实现

如果你设置属性的话,则必须在CustomControlActionList定义属性,方法也相同,代码如下


以上步骤完成以后就大功告成了,接着则与相关控件关联起来就可以了,效果图在上面已经看过了.

[DesignerAttribute(typeof(SampleControlDesigner))]


0
相关文章