none
wpf 如何让sql数据库的层级数据表结构表映射TreeView结构? RRS feed

  • 问题

  • sql数据库,里面有一个的根主表,根主表之下的所有从表的下一级从表……都是动态生成的数据表。所有从表的数据内容在根主表中都有

    目的:让该根主表的结构映射TreeView某个静态的根节点TreeViewItem对象。不是映射TreeView,是映射静态的根节点TreeViewItem对象。选中静态的根节点TreeViewItem,查询根主表的内容。选中静态的根节点TreeViewItem之下的任何一个TreeViewItem子节点(包括子节点的子节点),查询选中的TreeViewItem对象,就查询映射的从表。

    目前有什么好的设计思路或实例么?


    2016年9月7日 8:03

答案

  • Hi 轮回的齿轮,

    你可以将你的数据库主从表外键关联。 比如下面的2个表 Family 和 FamilyMember。 我们可以根据 FamilyID 键值来关联成员关系。并将他们绑定到TreeView层次结构中。

    public class Family
    {
        public Family()
        {
            this.Members = new ObservableCollection<FamilyMember>();
        }
        public int FamilyID { get; set; }
        public string Name { get; set; }
        public ObservableCollection<FamilyMember> Members { get; set; }
    }
    
    public class FamilyMember
    {
        public int FamilyID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    
    }

    TreeView设置层次模板如下,这样可以将主表作为跟节点,从表数据作为子节点绑定。

    <Grid Margin="10">
        <TreeView Name="trvFamilies"  TreeViewItem.Selected="trvFamilies_Selected">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Members}">
                    <StackPanel Orientation="Horizontal">
                        <!--<Image Source="" Margin="0,0,5,0" />-->
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text=" [" Foreground="Blue" />
                        <TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
                        <TextBlock Text="]" Foreground="Blue" />
                    </StackPanel>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type local:FamilyMember}">
                    <StackPanel Orientation="Horizontal">
                        <!--<Image Source="ser.png" Margin="0,0,5,0" />-->
                        <TextBlock Text="{Binding Name}" />
                        <TextBlock Text=" (" Foreground="Green" />
                        <TextBlock Text="{Binding Age}" Foreground="Green" />
                        <TextBlock Text=" years)" Foreground="Green" />
                    </StackPanel>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>

    这样,你点击任跟节点,或者子节点时,可以获取相关数据 (关联,查找数据源)。

    private void trvFamilies_Selected(object sender, RoutedEventArgs e)
    {
        TreeViewItem tvi = e.OriginalSource as TreeViewItem;
        if (tvi.HasItems)
        {
            Family folder = (Family)tvi.Header;
            string id = folder.FamilyID.ToString();
            string Name = folder.Name;
            MessageBox.Show(id + " - " + Name);
        }
        else
        {
            FamilyMember folder = (FamilyMember)tvi.Header;
            string id = folder.FamilyID.ToString();
            string Name = folder.Name;
            MessageBox.Show(id + " - " + Name);
        }
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年9月9日 1:30