从MVVM模式中的数据库递归填充WPF树视图 [英] Filling a WPF Treeview Recursively From a Database in an MVVM Pattern

查看:251
本文介绍了从MVVM模式中的数据库递归填充WPF树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Josh Smith的文章使用ViewModel模式简化WPF TreeView,使他的技术适应WPF,MVVM,Prism,Unity类型的应用程序。当我使用他的下载名为TreeVeiwWithViewModelDemo的样本硬编码数据源时,它工作得很好,但是我很难理解如何修改代码以便从SQL Server数据库递归地向Person对象添加数据。我希望有人已经这样做了,可以让我朝着正确的方向努力。



Josh的代码如下:

I have used Josh Smith's article, "Simplifying the WPF TreeView by Using the ViewModel Pattern", to adapt his technique to a WPF, MVVM, Prism, Unity type application. It works great when I use his sample "hardcoded" data source provided in his download called "TreeVeiwWithViewModelDemo", but I am stumped on how to modify the code in order to recursively add data to the Person object from a SQL Server database. I am hoping that somebody has already done that and can give me a push in the right direction.

Josh's code is as follows:

public static Person GetFamilyTree()
{
    // In a real app this method would access a database.
    return new Person
    {
        Name = "David Weatherbeam",
        Children =
        {
            new Person
            {
                Name="Alberto Weatherbeam",
                Children=
                { etc...





我需要修改它以访问数据库并返回列表Person,其定义如下:



I need to modify this to access a database and return the list Person which is defined as follows:

public class Person
{
    readonly List<Person> _children = new List<Person>();
    public IList<Person> Children
    {
        get { return _children; }
    }

    public string Name { get; set; }
}





...而且WPF项目模板如下所示:



... and the WPF item template looks like this:

<TreeView.ItemTemplate>
  <HierarchicalDataTemplate ItemsSource="{Binding Children}">
    <TextBlock Text="{Binding Name}" />
  </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

推荐答案

这不应该这么难。您需要做的就是将数据库中的所有人员存储起来,如下所示:

This shouldnt be so hard. All you need to do is store all the people in the database that could look something like this:
UniquePersonID  PersonNAme              Level              Parent
1               David Weatherbeam       0                  Nothing/Null
2               Alberto Weatherbeam     1                  1



等..



现在你可以轻松地检索建立连接所需的所有数据。



祝你好运:)


etc..

Now you could easely retrive all the data you need to make the connections.

So good luck to you :)


你应该看一下ObservableCollections和CollectionViewSources。

我会看到我能找到的关于这个主题的内容可能对你有帮助,但是平均时间寻找你的自我。

试试看看这个人做了什么。

调整TreeView - 第1部分 [ ^ ]



祝你好运:)
You should probably have a look at ObservableCollections and CollectionViewSources.
I'll see what I can find on the subject that may help you, but in the mean time have a search for your self.
Try have a look at what this guy did.
Tuning Up The TreeView - Part 1[^]

Good luck :)

这篇关于从MVVM模式中的数据库递归填充WPF树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆