结合从数据库层次树状(Binding hierarchical treeview from data

2019-09-27 02:08发布

我完全是新使用的TreeView。 我的解决方案有两个项目一个叫业务层即类库,从数据库和其他实际WPF应用程序本身获取数据。

我在我的两个非常简单的表Family数据库中具有数据如下:

我有我的业务层项目四类。

1)ChildrenModel.cs

public class ChildrenModel
{
    public string Name { get; set; }
    public int Age { get; set; }
    public double Income { get; set; }
    public string Address { get; set; }
    public int FId { get; set; }
}

2)FamilyModel.cs

public class FamilyModel
{
    public string Name { get; set; }

    public List<ChildrenModel> ChildenData { get; set; }
}

3)FamilyTemp.cs

public class FamilyTemp
{
    public int id { get; set; }
    public string name { get; set; }
}

4)最后DataRetrieval.cs从数据库中获取数据。 在这里,我有一个非常简单的存储过程,基本上结合了FamilyPerson表格。

我有以下类:

public class DataRetrieval
   {
        List<FamilyModel> familyList = new List<FamilyModel>();
        List<ChildrenModel> childList = new List<ChildrenModel>();
        List<FamilyTemp> familyNames = new List<FamilyTemp>();
        using (SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=|DataDirectory|\Families.mdf; Integrated Security=True; Connect Timeout=30;"))
        {

            con.Open();
            //int count = 1;
            //int temp = 1;
            SqlCommand cmd = new SqlCommand("sp_GetFamilies", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ChildrenModel cModel = new ChildrenModel();
                cModel.Name = dr["PersonName"].ToString();
                cModel.Age = Convert.ToInt32(dr["PersonAge"]);
                cModel.Address = dr["PersonAddress"].ToString();
                cModel.Income = double.Parse(dr["PersonIncome"].ToString());
                cModel.FId = Convert.ToInt32(dr["FamilyId"]);
                childList.Add(cModel);

            }
            dr.Close();
            SqlCommand cmd1 = new SqlCommand("Select Distinct * from Family", con);
            SqlDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                FamilyTemp ft = new FamilyTemp()
                {
                    id = Convert.ToInt32(dr1["Id"]),
                    name = dr1["Name"].ToString()
                };
                familyNames.Add(ft);
            }

            for (int i = 0; i < familyNames.Count; i++)
            {
                FamilyModel fModelResult = new FamilyModel();
                fModelResult.Name = familyNames[i].name;
                foreach (var item in childList.Where(x => x.FId == familyNames[i].id).ToList())
                {
                    fModelResult.ChildenData.Add(item);
                }
                familyList.Add(fModelResult);
            }

        }
        return familyList;

    }

}

现在,在主WPF应用程序我创建了一个名为TreeViewModel类,如下所示

public class TreeViewModel:INotifyPropertyChanged
{
    private List<FamilyModel> _familyList;
    public List<FamilyModel> FamilyList
    {
        get
        {
            return _familyList;
        }
        set
        {
            _familyList = value;
            NotifiyPropertyChanged("FamilyList");
        }
    }
    public TreeViewModel()
    {

        FamilyList = DataRetrieval.familyData();
    }



    void NotifiyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

现在,在我的主要窗口,我想的分层结构看起来在最后一个数字。 所以我有一些样品XAML如下。 DataContext的是TreeViewModel。 我应该提什么数据类型,它dosent似乎将数据绑定到TreeView。 请大家帮忙什么错误我在所作所为。得到一些错误的输出

<!--
self namespace is:
xmlns:self="clr-namespace:BusinessLayer;assembly=BusinessLayer"
-->
<Grid>
    <TextBlock Text="Family Tree" Foreground="Red"></TextBlock>
    <TreeView Margin="10" Height="200">
        <HierarchicalDataTemplate ItemsSource="{Binding FamilyList}" DataType="{x:Type self:FamilyModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type self:ChildrenModel}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"></TextBlock>
                <TextBlock Text="{Binding Age}"></TextBlock>
            </StackPanel>
        </DataTemplate>

    </TreeView>
</Grid>

我想树形视图以如下所示,

文章来源: Binding hierarchical treeview from database