none
WPF treeview显示问题 RRS feed

  • 问题

  • Wpf treeview的绑定使用了两个表,部门表,用户表,怎么做到两张表绑定,我只做出来一张表的效果,不知道怎么将用户和部门联系,想做出如图的效果:

    比如:

    信息部

           张三

    采购部

         李四

    望大侠指导!


    wy926

    2012年8月26日 15:49

答案

  • 嘎嘎,经过一上午的学习,终于有些灵感了,问题基本解决,主要是参照这个实现的:http://www.404qa.com/q-22302.html

     我贴代码喽!

    Model:
    
    public class Users
        {
            public int UserId { get; set; }
            public string UserName { get; set; }
            public int HospitalId { get; set; }
        }
    
        public class HospitalOrg
        {
            public int HospitalId { get; set; }
            public string HospitalName { get; set; }
            public int HospitalParentId { get; set; }
            public IList<Users> Userses { get; set; }
            public IList<HospitalOrg> HospitalOrgs { get; set; }
    
            public IList<object> Items
            {
                get
                {
                    IList<object> childNodes = new List<object>();
                    foreach (var group in this.HospitalOrgs)
                        childNodes.Add(group);
                    foreach (var entry in this.Userses)
                        childNodes.Add(entry);
    
                    return childNodes;
                }
            }
        }
    数据:
    
    public class TestData
        {
            public IList<HospitalOrg> HospitalOrgs = new List<HospitalOrg>();
    
            public void MyLoad()
            {
    
            }
    
            public void Load()
            {
                HospitalOrg hospitalOrg1 = new HospitalOrg() { HospitalId = 1, HospitalName = "GICU", HospitalOrgs = new List<HospitalOrg>(), HospitalParentId = 0, Userses = new List<Users>() };
                HospitalOrg hospitalOrg2 = new HospitalOrg() { HospitalId = 2, HospitalName = "ICU专科", HospitalOrgs = new List<HospitalOrg>(), HospitalParentId = 0, Userses = new List<Users>() };
    
                hospitalOrg1.HospitalOrgs.Add(new HospitalOrg()
                {
                    HospitalId = 3,
                    HospitalName = "SICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 1,
                    Userses = new List<Users>() { new Users() { HospitalId = 3, UserId = 1, UserName = "zhangsan" } }
                });
                hospitalOrg1.HospitalOrgs.Add(new HospitalOrg()
                {
                    HospitalId = 4,
                    HospitalName = "MICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 1,
                    Userses = new List<Users>() { new Users() { HospitalId = 4, UserId = 2, UserName = "lisi" } }
                });
                hospitalOrg2.HospitalOrgs.Add(new HospitalOrg()
                {
                    HospitalId = 5,
                    HospitalName = "RICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 2,
                    Userses = new List<Users>() { new Users() { HospitalId = 5, UserId = 3, UserName = "wangwu" } }
                });
                hospitalOrg2.HospitalOrgs.Add(new HospitalOrg()
                { 
                    HospitalId = 6,
                    HospitalName = "UICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 2,
                    Userses = new List<Users>() { new Users() { HospitalId = 6, UserId = 4, UserName = "zhaoliu" } }
                });
    
                HospitalOrgs.Add(hospitalOrg1);
                HospitalOrgs.Add(hospitalOrg2);
            }
    
    
        }


    窗口:
    
    <Window x:Class="WpfApplication2.MyTest.Test"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MyTest="clr-namespace:WpfApplication2.MyTest"
            Title="Test" Height="300" Width="300">
        <Grid>
            <TreeView Name="GroupView" Background="Pink" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type MyTest:HospitalOrg}" ItemsSource="{Binding Items}">
                        <TextBlock Text="{Binding Path=HospitalName}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type MyTest:Users}" >
                        <TextBlock Text="{Binding Path=UserName}" />
                    </DataTemplate>
                </TreeView.Resources>
            </TreeView>
        </Grid>
    </Window>
    
    
    
    窗口后台:
    
     public partial class Test : Window
        {
            public Test()
            {
                InitializeComponent();
                LoadView();
            }
    
            private void LoadView()
            {
                TestData data = new TestData();
                data.Load();
                GroupView.ItemsSource = data.HospitalOrgs;
    
            }
        }

    最后运行的效果:

    这个真的真的纠结我挺久的,把代码贴出来大家共享,希望对大家有帮助!


    wy926

    2012年8月27日 5:15

全部回复

  • 将两张表生成一个XML或者有级联关系的数据结构,之后使用HierarchicalDataTemplate设置绑定。

    关于HierarchicalDataTemplate你可以看MSDN:http://msdn.microsoft.com/zh-cn/library/system.windows.hierarchicaldatatemplate.aspx

    或者网上也有比较多的资料。


    Wanpeng wanpeng.ones@gmail.com

    2012年8月27日 4:53
  • 嘎嘎,经过一上午的学习,终于有些灵感了,问题基本解决,主要是参照这个实现的:http://www.404qa.com/q-22302.html

     我贴代码喽!

    Model:
    
    public class Users
        {
            public int UserId { get; set; }
            public string UserName { get; set; }
            public int HospitalId { get; set; }
        }
    
        public class HospitalOrg
        {
            public int HospitalId { get; set; }
            public string HospitalName { get; set; }
            public int HospitalParentId { get; set; }
            public IList<Users> Userses { get; set; }
            public IList<HospitalOrg> HospitalOrgs { get; set; }
    
            public IList<object> Items
            {
                get
                {
                    IList<object> childNodes = new List<object>();
                    foreach (var group in this.HospitalOrgs)
                        childNodes.Add(group);
                    foreach (var entry in this.Userses)
                        childNodes.Add(entry);
    
                    return childNodes;
                }
            }
        }
    数据:
    
    public class TestData
        {
            public IList<HospitalOrg> HospitalOrgs = new List<HospitalOrg>();
    
            public void MyLoad()
            {
    
            }
    
            public void Load()
            {
                HospitalOrg hospitalOrg1 = new HospitalOrg() { HospitalId = 1, HospitalName = "GICU", HospitalOrgs = new List<HospitalOrg>(), HospitalParentId = 0, Userses = new List<Users>() };
                HospitalOrg hospitalOrg2 = new HospitalOrg() { HospitalId = 2, HospitalName = "ICU专科", HospitalOrgs = new List<HospitalOrg>(), HospitalParentId = 0, Userses = new List<Users>() };
    
                hospitalOrg1.HospitalOrgs.Add(new HospitalOrg()
                {
                    HospitalId = 3,
                    HospitalName = "SICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 1,
                    Userses = new List<Users>() { new Users() { HospitalId = 3, UserId = 1, UserName = "zhangsan" } }
                });
                hospitalOrg1.HospitalOrgs.Add(new HospitalOrg()
                {
                    HospitalId = 4,
                    HospitalName = "MICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 1,
                    Userses = new List<Users>() { new Users() { HospitalId = 4, UserId = 2, UserName = "lisi" } }
                });
                hospitalOrg2.HospitalOrgs.Add(new HospitalOrg()
                {
                    HospitalId = 5,
                    HospitalName = "RICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 2,
                    Userses = new List<Users>() { new Users() { HospitalId = 5, UserId = 3, UserName = "wangwu" } }
                });
                hospitalOrg2.HospitalOrgs.Add(new HospitalOrg()
                { 
                    HospitalId = 6,
                    HospitalName = "UICU",
                    HospitalOrgs = new List<HospitalOrg>(),
                    HospitalParentId = 2,
                    Userses = new List<Users>() { new Users() { HospitalId = 6, UserId = 4, UserName = "zhaoliu" } }
                });
    
                HospitalOrgs.Add(hospitalOrg1);
                HospitalOrgs.Add(hospitalOrg2);
            }
    
    
        }


    窗口:
    
    <Window x:Class="WpfApplication2.MyTest.Test"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MyTest="clr-namespace:WpfApplication2.MyTest"
            Title="Test" Height="300" Width="300">
        <Grid>
            <TreeView Name="GroupView" Background="Pink" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding}">
                <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type MyTest:HospitalOrg}" ItemsSource="{Binding Items}">
                        <TextBlock Text="{Binding Path=HospitalName}" />
                    </HierarchicalDataTemplate>
                    <DataTemplate DataType="{x:Type MyTest:Users}" >
                        <TextBlock Text="{Binding Path=UserName}" />
                    </DataTemplate>
                </TreeView.Resources>
            </TreeView>
        </Grid>
    </Window>
    
    
    
    窗口后台:
    
     public partial class Test : Window
        {
            public Test()
            {
                InitializeComponent();
                LoadView();
            }
    
            private void LoadView()
            {
                TestData data = new TestData();
                data.Load();
                GroupView.ItemsSource = data.HospitalOrgs;
    
            }
        }

    最后运行的效果:

    这个真的真的纠结我挺久的,把代码贴出来大家共享,希望对大家有帮助!


    wy926

    2012年8月27日 5:15
  • 将两张表生成一个XML或者有级联关系的数据结构,之后使用HierarchicalDataTemplate设置绑定。

    关于HierarchicalDataTemplate你可以看MSDN:http://msdn.microsoft.com/zh-cn/library/system.windows.hierarchicaldatatemplate.aspx

    或者网上也有比较多的资料。


    Wanpeng wanpeng.ones@gmail.com

    谢谢喽!

    wy926

    2012年8月27日 5:15
  • 很高兴你解决了这个问题并和我们分享了答案, 谢谢:)


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    2012年8月27日 8:05
    版主
  • 很高兴你解决了这个问题并和我们分享了答案, 谢谢:)


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    因为这个网上很少,而且也很实用,有必要大家一起分享,彼此学习!

    wy926

    2012年8月27日 9:31