Listview group in uwp

前端 未结 1 718
野趣味
野趣味 2021-02-10 10:49

I have a simple view with a ListView, I would like the ListViewhas two groups, a group for items with property complete= 1 and another gro

1条回答
  •  时光说笑
    2021-02-10 10:55

    I have a simple view with a ListView, I would like the ListViewhas two groups, a group for items with property complete= 1 and another group with complete= 0.

    First, use a CollectionViewSource for content that presents a list of items that can be grouped or sorted.

    
        
    
    

    Then, get the data, group the data and set the grouped data to the CollectionViewSource in code behind.

    Following is the sample code I have verified:

    MainPage.xaml

    
        
        
    
    
    
        
            
                
                    
                        
                    
                
            
            
                
                    
                        
                        
                    
                
            
        
    
    

    MainPage.xaml.cs

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            //Get the data
            List myClasses = new List();
            myClasses.Add(new MyClass { Name = "A", Complete = false });
            myClasses.Add(new MyClass { Name = "B", Complete = true });
            myClasses.Add(new MyClass { Name = "C", Complete = true });
            myClasses.Add(new MyClass { Name = "D", Complete = false });
            myClasses.Add(new MyClass { Name = "E", Complete = true });
            myClasses.Add(new MyClass { Name = "F", Complete = false });
            //Group the data
            var groups = from c in myClasses
                         group c by c.Complete;
            //Set the grouped data to CollectionViewSource
            this.cvs.Source = groups;
        }
    }
    

    Following is the output:

    0 讨论(0)
提交回复
热议问题