How do I use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities?

后端 未结 3 1653
醉话见心
醉话见心 2020-12-10 12:55

I\'ve got a Page class in my .edmx ADO.NET Entity Data Model file with with Parent and Children properties. It\'s for a hierarchy of Pages.

removed dead ImageSh

3条回答
  •  一生所求
    2020-12-10 13:29

    A different way: (well, very similar, but slightly different)

    In your Window Load function:

    PageEntities db = new PageEntities();
    TreeViewPages.ItemsSource = db.Page.Where(u=>u.Parent==null);
    

    Create a new file Page.cs

    public partial class Page {
        public ObjectQuery LoadedChildren {
            get {
                var ret = Children;
                if(ret.IsLoaded==false) ret.Load();
                return ret;
            }
        }
    }
    

    In your XAML:

    
        
            
                
            
        
    
    

    Its not tested, but you should get the general idea.

提交回复
热议问题