可以重寫ToString
方法來顯示你想要什麼屬性,例如,假設我們有下面的類作爲SelectedObject
您propertyGrid
控制:
public class Company
{
[Category("Main")]
[DisplayName("Name")]
[Description("Property description")]
public String Name { get; set; }
[Category("Main")]
[DisplayName("Type")]
[Description("Property description")]
public String Type { get; set; }
[Category("Main")]
[DisplayName("Something")]
[Description("Property description")]
public bool Something { get; set; }
[Category("Main")]
[DisplayName("Director")]
[Description("Property description")]
[ItemsSource(typeof(EmployeList))]
public Employe Director { get; set; }
}
收集應被定義爲跟隨
public class EmployeList : IItemsSource
{
public Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection GetValues()
{
Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection employe = new Xceed.Wpf.Toolkit.PropertyGrid.Attributes.ItemCollection();
employe.Add(new Employe()
{
Name = "Name1",
Rank = "Rank1",
Age=40,
}); employe.Add(new Employe()
{
Name = "Name2",
Rank = "Rank2",
Age=40,
}); employe.Add(new Employe()
{
Name = "Name3",
Rank = "Rank3",
Age=40,
});
return employe;
}
}
和Employe
類應重寫Tostring
方法
public class Employe
{
public String Name { get; set; }
public String Rank { get; set; }
public int Age { get; set; }
public override string ToString()
{
return Name;
}
}
XAML
<xctk:PropertyGrid Name="pg" SelectedObject="{Binding SelectedCompany}" AutoGenerateProperties="True" >
</xctk:PropertyGrid>
,其結果是你在找什麼
謝謝您的回答。我已經嘗試過這種解決方案,問題是一旦進入ItemCollection,就會創建一個新的Item類並將其輸入到集合中。所以調用的ToString函數仍然是Item的一個。 – petric