渴望加载级联组合框

时间:2014-03-25 20:54:12

标签: c# entity-framework

我有一个实体框架应用程序,我在Customer和Projects之间有一对多的关系。在级联组合框中访问数据。我最初使用延迟加载,一切正常。由于我现在必须序列化我的数据,我需要以另一种方式加载我的数据。我想使用急切加载,但我不确定如何在我的情况下实现它。在我尝试使用以下内容访问的Projects属性中:

 var a = context.Customers.Include("Project");
            projects = (ObservableCollection<Project>)a;

但是我收到转换类型错误。有人建议我访问产品数据吗?感谢。

public class ViewModel:INotifyPropertyChanged
{
    RevisionModelContainer context = new RevisionModelContainer();

    public ViewModel()
    {
        Customers = new ObservableCollection<Customer>();            
    }
 private ObservableCollection<Customer> customers;
    public ObservableCollection<Customer> Customers
    {
        get 
        {         
            return customers;            
        }            
        set
        {
            customers = value;
            OnPropertyChanged("Customers");               
        }
    }

    private ObservableCollection<Project> projects;
    public ObservableCollection<Project> Projects 
    {
        get
        {
            var a = context.Customers.Include("Project");
            projects = (ObservableCollection<Project>)a;
            return projects;             
        }
        set
        {
            projects = value;
            OnPropertyChanged("Projects");
        }
    }

 private Customer selectedCustomer;
    public Customer SelectedCustomer
    {
        get { return selectedCustomer; }
        set
        {
            try
            {
                selectedCustomer = value;
                OnPropertyChanged("SelectedCustomer");
                this.Projects = selectedCustomer.Projects;
                OnPropertyChanged("Projects");
            }
            catch (Exception ex) 
            {
                Debug.WriteLine("Seleceted Customer: " + ex.Message);
            }
        }
    }

    private Project selectedProject;
    public Project SelectedProject
    {
        get { return selectedProject; }
        set
        {
            try
            {
                selectedProject = value;
                OnPropertyChanged("SelectedProject");
                this.Programs = selectedProject.Programs;
                OnPropertyChanged("Programs");
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Selected Project: " + ex.Message);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您的“项目”和“客户”属性应分别为IEnumerable<Project>IEnumerable<Customer>类型。您不需要向ObservableCollection投射任何内容。您需要使用ObservableCollections的唯一时间是视图需要对集合的内容做出反应。在您的情况下,视图只需要知道Projects属性已更改为不同的项目列表。

这就是我的意思:

public class ViewModel : INotifyPropertyChanged
{
    RevisionModelContainer context = new RevisionModelContainer();

    public ViewModel() 
    {
        Customers = context.Customers.Include("Project");
    }

    public IEnumerable<Customer> Customers { get; private set; }

    public IEnumerable<Project> Projects { get; private set; }

    private Customer selectedCustomer;
    public Customer SelectedCustomer
    {
        get 
        { 
            return selectedCustomer; 
        }
        set
        {
            selectedCustomer = value;
            OnPropertyChanged("SelectedCustomer");

            // reset the projects.
            this.SelectedProject = null;
            this.Projects = selectedCustomer.Projects;
            OnPropertyChanged("Projects");
        }
    }

    private Project selectedProject;
    public Project SelectedProject
    {
        get 
        { 
            return selectedProject; 
        }
        set
        {
            selectedProject = value;
            OnPropertyChanged("SelectedProject");
        }
    }
}

从我可以告诉你的用例,这应该工作。如果您在视图模型中动态添加删除项目来自客户,项目或程序集合,则只需要可观察的集合:Projects.Add(new Project());。< / p>

如果你所做的只是分配像这个Projects = SelectedCustomer.Projects这样的属性,则不需要使用可观察的集合,你只需要为你更新的属性引发propertychanged事件。