每个对象的CollectionView?

时间:2009-11-04 17:26:11

标签: c# wpf

我有以下课程

public class MyTask 
{
    public int CustomerID { get; set; }
    public int ProjectID { get; set; }
}

public List<Project> AllProjects { get; set; }
public class Project 
{
    public string ProjectName { get; set; }
    public int CustomerID { get; set; }
}

现在在程序启动时,我将所有可用项目加载到“AllProjects”中。然后我将Collection绑定到ComboBox,其中User首先必须输入CustomerID,并且根据该值,Project的ComboBox会发生变化。你认为最好的方法是什么?为每个MyTask使用CollectionView?

我现在正在做的是在MyTask中有一个List AvailableProjects,每次更改MyTask.CustomerID时都会更改。 e.g。

public int CustomerID { 
    get { return _customerID; }
    set { _customerID = value; UpdateAvailableProjects(); }
}
private void UpdateAvailableProjects()
{
   //Loop trough static.Main.AllProjects and check if Project.CustomerID == this.CustomerID); 
}

1 个答案:

答案 0 :(得分:0)

为什么不创建一个Customer类,然后从中获取所有可以在组合中加载的相关项目。

public class Customer
{
    private int customerId = 0;
    public int CustomerId
    {
        get
        {
            return customerId;
        }
        set
        {
            customerId = value;
        }
    }

    public List<Project> availableProjects
    {
        get
        {
            return getCustomerRelatedProjects();
        }
    }

    //you change the accessor here according to the needs
    private List<Project> getCustomerRelatedProjects()
    {
        //do the processing here for Database hit or from the list you load at the program load etc.
        return new List<Project>();
    }

}

public class Project 
{
    int projectId = 0;

    public int ProjectId
    {
        get
        {
            return projectId;
        }
        set
        {
            projectId = value;
        }
    }

    string projName = string.Empty;
    public string ProjectName
    {
        get
        {
            return projName;
        }
        set
        {
            projName = value;
        }
    }
}