PropertyGrid 清除一个值

本文关键字:一个 清除 PropertyGrid | 更新日期: 2023-09-27 18:37:25

我有一个属性网格,它以空值开头。这些值是可选的。如果用户意外地在其中一个属性中输入数据并将其清除,则网格不允许他们清除它。例如,如果属性的类型为 uint32,则它需要 uint32 范围内的内容。如何使网格接受值为空?

PropertyGrid 清除一个值

您可以使用可为 null 的类型声明您的属性(例如 int? )以使属性网格接受空值。可为空的类型表示基础数据类型的范围加上 null 值。

下面是一个使用可为空的 int 的小示例:

// Your class for which the property grid should display properties
public class YourClass
{    
  public int? MyIntValue        // Nullable int
  {
    get;set;     
  }
  public string MyStringValue
  {
    get;set;
  }
}
public partial class Form1 : Form
{
  private YourClass yourClass;
  // In your winform
  private void Form1_Load(object sender, EventArgs e)
  {
    yourClass = new YourClass();
    // Set selected object
    propertyGrid1.SelectedObject = yourClass;
  }
}