2010-04-20 59 views
0

我在我的数据库表5个具有以下字段:LINQ和WPF数据绑定与关联数据库

  • 客户:ID(INT),名称(字符串)
  • 项目:ID(INT)名称(字符串)
  • 任务:ID(INT),名称(字符串)
  • Customer_Project:ID(INT),客户ID(INT)专案编号(INT)
  • Project_Task:ID(INT),专案编号(INT) ,TaskID(int)

最后两个表创建关联,以便任意数量的客户可以与任意数量的项目关联。与项目和任务相同。

我试图遵守良好的MVVM标准。在我的WPF控件中,我已将ViewBox中的ListBoxes绑定到我的ViewModel中的Customers,Tasks和Projects。我有一个TwoWay SelectedCustomer绑定属性。在我的模型中,我有所有这些元素的对象,并可以通过ID,名称等引用它们。

我想要做的是创建SelectedProjects和SelectedTasks的列表,其中用户只从列表,然后根据Customer_Project将这两个列表框与“关联”项目一起填充,同样也与Projects_Tasks一起填充。

我真的不想在使用一堆foreach循环中破解这个功能,我认为必须使用Linq连接并绑定到动态ObservableCollections。

任何想法?

+0

使用LINQ加入是你想做的事...你询问如何做榜样一个连接,或更多? – Brent 2010-04-20 21:22:42

+0

最好是对我的方法进行更全面,更全面的分析,也许是使用WPF控件处理关联数据库的最佳方法的高级解决方案。高层次的建议很好,我可以查询细节。然而,使用上面的表格进行linq连接的适用示例当然会非常好:P – bufferz 2010-04-21 01:34:16

+0

我的答案适合您吗?如果是这样,请将其标记为已回答,否则请告诉我您是否仍有问题。 – Brent 2010-05-05 16:16:41

回答

1

首先,下载MVVM template for Visual Studio 2008.这会给你一个ViewModelBase类,它允许你的ViewModel继承像PropertyChange通知等东西。接下来,做这样的事情:

查看:

<StackPanel Orientation="Horizontal"> 
    <ListBox ItemsSource="{Binding Customers}" 
      SelectedItem="{Binding SelectedCustomer}" Width="100"/> 
    <ListBox ItemsSource="{Binding Projects}" 
      SelectedItem="{Binding SelectedProject}" Width="100"/> 
    <ListBox ItemsSource="{Binding Tasks}" Width="100"/> 
</StackPanel> 

视图模型:

/// <summary> 
/// MyViewModel class 
/// </summary> 
public class MyViewModel : ViewModelBase 
{ 
    private DbDataContext _dc; 

    /// <summary> 
    /// Default constructor 
    /// </summary> 
    public MyViewModel() 
    { 
     _dc = new DbDataContext(); 

     Customers = new ObservableCollection<Customer>(
      (from c in _dc.Customers select c).ToList()); 
    } 

    /// <summary> 
    /// Customer List 
    /// </summary> 
    private ObservableCollection<Customer> _customers; 
    public ObservableCollection<Customer> Customers 
    { 
     get { return _customers; } 
     set 
     { 
      _customers = value; 

      // Notify the UI that the collection has changed 
      OnPropertyChanged("Customers"); 
     } 
    } 

    /// <summary> 
    /// When the user selects a customer from the list, 
    /// populate the list of projects for the customer 
    /// </summary> 
    private Customer _selectedCustomer; 
    public Customer SelectedCustomer 
    { 
     get { return _selectedCustomer; } 
     set 
     { 
      _selectedCustomer = value; 
      Projects = new ObservableCollection<Project>(
       (from p in _dc.Projects join c in _dc.Customer_Projects 
       on p.ID equals c.ProjectID where c.CustomerID == SelectedCustomer.ID 
       select p).ToList()); 
     } 
    } 

    /// <summary> 
    /// When the user selects a project from the list, 
    /// populate the list of tasks for the project 
    /// </summary> 
    private Project _selectedProject; 
    public Project SelectedProject 
    { 
     get {return _selectedProject;} 
     set 
     { 
      _selectedProject = value; 
      Tasks = new ObservableCollection<Task>(
       (from t in _dc.Tasks join p in _dc.Project_Tasks 
       on t.ID equals p.TaskID where p.ProjectID == SelectedProject.ID 
       select t).ToList()); 
     } 
    } 

    /// <summary> 
    /// Project List 
    /// </summary> 
    private ObservableCollection<Project> _projects; 
    public ObservableCollection<Project> Projects 
    { 
     get { return _projects; } 
     set 
     { 
      _projects = value; 

      // Notify the UI that the collection has changed 
      OnPropertyChanged("Projects"); 
     } 
    } 

    /// <summary> 
    /// Task List 
    /// </summary> 
    private ObservableCollection<Task> _tasks; 
    public ObservableCollection<Task> Tasks 
    { 
     get { return _tasks; } 
     set 
     { 
      _tasks = value; 

      // Notify the UI that the collection has changed 
      OnPropertyChanged("Tasks"); 
     } 
    } 
}