2011-09-23 76 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