2012-06-15 71 views
1

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

回答

2

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

下面是一个使用可空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; 
    } 
} 
+0

你不想做的属性自动实现的? – abatishchev

+0

@abatishchev:完成! – Hans

+0

对不起,迟到的回应。我最感兴趣的是将它用于不仅仅是值类型的类型。防爆。 System.uint32。 – Murlex