现在的位置: 首页 > 综合 > 正文

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

2012年02月29日 ⁄ 综合 ⁄ 共 2818字 ⁄ 字号 评论关闭

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

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

this.PropTableColumn.SelectedObject = ... ;

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

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

public class ABSControlsSettings

    
{

        
public ABSControlsSettings()

        
{

        }
        

    }

那么这个类就可以用于绑定,只是什么属性都没有而已,OK,现在我们加入一个ID属性的维护输入,其代码和加入一个普通的类属性没有什么区别

private string _ControlID;

public string ControlID

        
{

            
get return _ControlID; }

            
set { _ControlID = value; }

        }

此时我们可以通过添加一系列属性来完善它,如

CategoryAttribute("System")

ReadOnlyAttribute(false)

DescriptionAttribute("Controll's ID")

这个只是最普通的属性,如果属性需要设置true or false,那么只需要这样设置

private bool _Needcheck;

public bool Needcheck

        
{

            
get return _Needcheck; }

            
set { _Needcheck = value; }

        }

如果是一个需要进行多项选择,如同BorderStyle之类的参数,可以通过枚举属性来实现

private ControlTypeEnum _ControlType = ControlTypeEnum.Label;

public ControlTypeEnum ControlType

        
{

            
get return _ControlType; }

            
set { _ControlType = value;}

        }


public enum ControlTypeEnum

{

        
/// <summary>

        
/// 标签

        
/// </summary>


        Label = 1,

        
/// <summary>

        
/// 文本框

        
/// </summary>


        TextBox = 2

}

当然还不能少了Collection类型的数据设置,譬如ListBox的Items之类,我们可以将属性设置如下:

private ListItemCollection _ValueList = new ListItemCollection();

public ListItemCollection ValueList

        
{

            
get return _ValueList; }

            
set { _ValueList = value; }

        }




/// <summary>

    
/// 单项

    
/// </summary>


    public class ListItem

    
{

        
public ListItem()

        
{

            
//

        }



        
private string _Text = "";

        
private string _Value = "";


        [CategoryAttribute(
"系统"),

        ReadOnlyAttribute(
false),

        DescriptionAttribute(
"子项文本")]

        
public string Text

        
{

            
get return _Text; }

            
set { _Text = value; }

        }



        [CategoryAttribute(
"系统"),

        ReadOnlyAttribute(
false),

        DescriptionAttribute(
"子项数值")]

        
public string Value

        
{

            
get return _Value; }

            
set { _Value = value; }

        }


    }



    
public class ListItemCollection : System.Collections.CollectionBase

    
{

        
public ListItemCollection()

        
{

            
//

        }



        
public void Add(ListItem _LI)

        
{

            
base.InnerList.Add(_LI);

        }



        
public void Remove(ListItem _LI)

        
{

            
base.InnerList.Remove(_LI);

        }



        
public ListItem this[int index]

        
{

            
set

            
{

                InnerList[index] 
= value;

            }


            
get

            
{

                
return (ListItem)InnerList[index];

            }


        }


    }


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

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

抱歉!评论已关闭.