这几天的工作用到了Winform的PropertyGrid,说说心得~

      PropertyGrid的功能不用我多说了,看名字就可以知道个大概,在这里就谈谈它的一些功能使用。

假设目前有一个PropertyGrid控件PropTableColumn,那么进行属性绑定只需要一句话:

this.PropTableColumn.SelectedObject = ... ;

其中...表示需要进行绑定的对象,PropertyGrid使用的广泛性也主要体现在对象的设计上.

假设我们目前设置了一个类ABSControlsSettings用于绑定,代码如下:

None.gif public   class  ABSControlsSettings
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public ABSControlsSettings()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif    }
那么这个类就可以用于绑定,只是什么属性都没有而已,OK,现在我们加入一个ID属性的维护输入,其代码和加入一个普通的类属性没有什么区别
None.gif private   string  _ControlID;
None.gif
public   string  ControlID
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ControlID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _ControlID = value; }
ExpandedBlockEnd.gif        }
此时我们可以通过添加一系列属性来完善它,如
CategoryAttribute("System")
ReadOnlyAttribute(false)
DescriptionAttribute("Controll's ID")

这个只是最普通的属性,如果属性需要设置true or false,那么只需要这样设置
None.gif private   bool  _Needcheck;
None.gif
public   bool  Needcheck
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Needcheck; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Needcheck = value; }
ExpandedBlockEnd.gif        }

如果是一个需要进行多项选择,如同BorderStyle之类的参数,可以通过枚举属性来实现
None.gif private  ControlTypeEnum _ControlType  =  ControlTypeEnum.Label;
None.gif
public  ControlTypeEnum ControlType
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ControlType; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _ControlType = value;}
ExpandedBlockEnd.gif        }

None.gif
public   enum  ControlTypeEnum
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 标签
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        Label = 1,
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 文本框
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        TextBox = 2
ExpandedBlockEnd.gif}

当然还不能少了Collection类型的数据设置,譬如ListBox的Items之类,我们可以将属性设置如下:
None.gif private  ListItemCollection _ValueList  =   new  ListItemCollection();
None.gif
public  ListItemCollection ValueList
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _ValueList; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _ValueList = value; }
ExpandedBlockEnd.gif        }

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// <summary>
InBlock.gif    
/// 单项
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  ListItem
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public ListItem()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
private string _Text = "";
InBlock.gif        
private string _Value = "";
InBlock.gif
InBlock.gif        [CategoryAttribute(
"系统"),
InBlock.gif        ReadOnlyAttribute(
false),
InBlock.gif        DescriptionAttribute(
"子项文本")]
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Text; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Text = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [CategoryAttribute(
"系统"),
InBlock.gif        ReadOnlyAttribute(
false),
InBlock.gif        DescriptionAttribute(
"子项数值")]
InBlock.gif        
public string Value
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _Value; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _Value = value; }
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
public   class  ListItemCollection : System.Collections.CollectionBase
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public ListItemCollection()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public void Add(ListItem _LI)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.InnerList.Add(_LI);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Remove(ListItem _LI)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.InnerList.Remove(_LI);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public ListItem this[int index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                InnerList[index] 
= value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return (ListItem)InnerList[index];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

这些只是一些用户扩展部分的属性定义,还有一些系统级的属性可以直接设置,如Font设置颜色等,大家可以自己去试~

总的说来,PropertyGrid是一个弹性不错的控件,灵活使用可以让我们的UI更加容易操作~




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值