2011-09-23 77 views
3

我有一個Employee類。數據庫中有許多部門維護,一名僱員可能只屬於一個特定的部門。PropertyGrid C中的列表#

public class Employee 
{ 
    private string name; 
    private int depID; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    public int DepartmentID 
    { 
     get { return depID; } 
     set { depID = value; } 
    } 
} 

public class Department 
{ 
    private int depID; 
    private string depName; 

    public int DepartmentID 
    { 
     get { return depID; } 
    } 

    public int DepartmentName 
    { 
     get { return depName; } 
     set { depName = value; } 
    } 
} 

如何在部件PropertyGrid中顯示對象Employee作爲將顯示爲組合框的屬性之一?

可能嗎?還是有更好的實施? 預先感謝您的意見。

+0

當你說*任何更好的實現*,你的意思是,你可能放棄PropertyGrid中做到這一點? PropertyGrid中的組合框並不像看起來那麼簡單,所以任何其他解決方案都不會更糟。 –

回答

4

我繼續爲你起草一個實驗(對於我自己,因爲我從來沒有這樣做過)。它使用Linq爲這個特定的解決方案來填充組合框,但我相信你可以用其他方式填充它。

我的文檔從here來到下的第添加域列表和簡單的下拉屬性的支持

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 

public class Employee : StringConverter 
{ 
    DataClasses1DataContext mydb = new DataClasses1DataContext(); 

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
    { 
     return true; 
    } 

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
    { 
     var a = (from u in mydb.Customers select u.CustomerID).ToArray(); 
     return new StandardValuesCollection(a); 
    } 

    public string Name { get; set; } 

    [TypeConverter(typeof(Employee)), CategoryAttribute("Document Settings")] 
    public string DepartmentID { get; set; } 
} 

在我所選擇的形態負載:

private void Form1_Load(object sender, EventArgs e) 
{ 
    Employee temp = new Employee(); 
    propertyGrid1.SelectedObject = temp; 
} 

我希望這是什麼你正在尋找。值得注意的是,如果您願意,可以將StringConverter更改爲TypeConverter,但是我使用了String,因爲我正在處理的字段是一個字符串。

enter image description here

+0

Alt-PrntScrn將只將活動窗口複製到剪貼板,而不是整個桌面。 – LarsTech

+0

@ Lars Pro提示被讚賞:) – KreepN

+0

我個人認爲這是一種反觀模式,讓視圖實現慢慢滲透到一個領域模型中,'Employee'似乎是我的一部分。如果'Employee'是一個視圖模型或DTO,那麼這可能是正確的,但是我會使用不同的命名約定。 –

2

您可以通過實現的TypeConverter