3
如何在c#中獲取屬性網格物品和物品的值? 例如:如何獲取PropertyGrid的單元格值(c#)?
Name : Ali
LastName : Ahmadi
(名稱和LastName是PropertyGrid中的2個屬性)
如何在c#中獲取屬性網格物品和物品的值? 例如:如何獲取PropertyGrid的單元格值(c#)?
Name : Ali
LastName : Ahmadi
(名稱和LastName是PropertyGrid中的2個屬性)
正確答案是:
private void button1_Click(object sender, EventArgs e)
{
GridItem gi = propertyGrid1.SelectedGridItem;
while (gi.Parent != null)
{
gi = gi.Parent;
}
foreach (GridItem item in gi.GridItems)
{
ParseGridItems(item); //recursive
}
}
private void ParseGridItems(GridItem gi)
{
if (gi.GridItemType == GridItemType.Category)
{
foreach (GridItem item in gi.GridItems)
{
ParseGridItems(item);
}
}
textBox1.Text += "Lable : "+gi.Label + "\r\n";
if(gi.Value != null)
textBox1.Text += "Value : " + gi.Value.ToString() + "\r\n";
}
PropertyGrid
是剛剛超過一個對象的組件的模型表示的圖。而不是看網,我會說:看看組件的模型,例如:
var props = TypeDescriptor.GetProperties(obj);
foreach(var prop in props) {
string name = prop.DisplayName;
if(string.IsNullOrEmpty(name)) name = prop.Name;
Console.WriteLine("{0}: {1}", name, prop.GetValue(obj));
}