Linq和WPF数据绑定与关联数据库

时间:2010-04-20 19:47:17

标签: wpf linq mvvm

我的数据库中有5个表,其中包含以下字段:

  • 客户:ID(int),名称(字符串)
  • 项目:ID(int),名称(字符串)
  • 任务:ID(int),名称(字符串)
  • Customer_Project:ID(int),CustomerID(int)ProjectID(int)
  • Project_Task:ID(int),ProjectID(int),TaskID(int)

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

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

我想要做的是创建SelectedProjects和SelectedTasks的列表,其中用户只从列表中选择一个客户,然后两个列表框根据Customer_Project填充“关联的”项目,同样使用Projects_Tasks填充。 / p>

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

首先,为Visual Studio 2008下载MVVM template。这将为您提供一个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");
        }
    }
}