c# propertyGrid 出现checkbox属性的效果

来源:互联网 发布:java web出卷考试系统 编辑:程序博客网 时间:2024/06/05 17:07

怎样让C# propertyGrid控件,对于其bool型属性,让其以checkbox的样式出现如下图所示

先自定义一个类,用于更改propertyGrid属性样式。这是某国外网站的描述

Display a check-box next to a Boolean value in the property grid.

This is a bit of a cheat because it is unfortunately not possible to host a control in the value area of the PropertyGrid. It is however possible to draw in that area if we have the correct UITypeEditor.

UITypeEditor has a couple of methods GetPaintValueSupported andPaintValue that enable you to draw a preview of the value. This is how the thumbnail of images is displayed next to image data in the property grid. By creating an editor and assigning it to your boolean value you can display a checkbox which shows a check-mark whenever the value is set to true.

其实就是通过C#画笔,画一个checkbox在它的bool值得属性里。具体代码如下

  public class CheckBoxInPropertyGridEditor : UITypeEditor

  {

    public overridebool GetPaintValueSupported(ITypeDescriptorContext context)

    {

      return true;

    }

    public overridevoid PaintValue(PaintValueEventArgs e)

    {

      ControlPaint.DrawCheckBox(e.Graphics,e.Bounds,((CheckBoxInPropertyGrid)e.Context.Instance).Isalarm? ButtonState.Checked : ButtonState.Normal);

    }

  }

这里先自定义一个类,将会应用到propertyGrid的bool值属性上。如见

public class CheckBoxInPropertyGrid

  {

    bool _cb;

    [Editor(typeof(CheckBoxInPropertyGridEditor),typeof(System.Drawing.Design.UITypeEditor))]

    public bool Isalarm

    {

      get{return _cb;}

      set{_cb=value;}

    }

  }

然后在设置propertyGrid的属性

propertyGrid1.SelectedObject=new CheckBoxInPropertyGrid();

这样propertyGrid的Isalarm属性就会显示出checkBox的效果。





2 0
原创粉丝点击