【问题标题】:Linq and WPF databinding with an association database与关联数据库的 Linq 和 WPF 数据绑定
【发布时间】:2010-04-20 19:47:16
【问题描述】:

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

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

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

我正在努力遵守良好的 MVVM 标准。在我的 WPF 控件中,我已经将视图中的 ListBoxes 绑定到我的 ViewModel 中的客户、任务和项目。我有一个 TwoWay SelectedCustomer 绑定属性。在我的模型中,我拥有所有这些元素的对象,并且可以通过 ID、名称等引用它们。

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

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

有什么想法吗?

【问题讨论】:

  • 使用 Linq 连接是您想要做的......您是在询问如何进行连接的示例,还是更多?
  • 最好是对我的方法进行更全面、全面的分析,也许是使用 WPF 控件处理关联数据库的最佳方法的高级解决方案。高层次的建议很好,我可以查找详细信息。但是,使用我上面的表的 linq 连接的适用示例当然会非常好:P
  • 我的回答对你有用吗?如果是,请将其标记为已回答,否则如果您仍有疑问,请告诉我。

标签: wpf linq mvvm


【解决方案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");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-09-30
    • 2010-09-17
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多