c# propertyGrid 出现checkbox属性的效果

怎样让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的效果。





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值