WPF: Bind Collection with Collection to a ListBox with groups

前端 未结 2 827
轮回少年
轮回少年 2021-02-04 16:37

sometimes WPF is too complex for me. I\'ve got my \"Window1\" holding a collection of \"Group\"s. \"Group\" is a class with a collection of \"Person\"s. In the end this should b

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-04 17:07

    Well, I am not sure if this is what you want achieve but here is a way that you can try:

    Assuming your classes are like these:

    public class Group
    {
        public string Name { get; set; }
        public List Contacts { get; set; }
    }
    
    public class Contact
    {
        public string Name { get; set; }
        public bool IsOnline { get; set; }
    }
    

    You can set ListBox.ItemTemplate as another ListBox bind to Contacts property, like:

    
        
            
        
    
    
    
        
            
        
    
    
    
        
            
        
        
            
                
                    
                        
                            
                        
                    
                
            
        
    
    

    You have to style the inner listbox a little bit.

    Edit: Another solution by using TreeView

    
       
    
    
    
        
            
                
            
        
    
    

提交回复
热议问题