2010-04-20 46 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"); 
     } 
    } 
}