Merging cells in WPF DataGrid vertically

后端 未结 2 1619
情深已故
情深已故 2021-01-13 15:41

I want to make a DataGrid in WPF, where some of the cells will \"merge together\", if they are alike.

Example:

+---------+------+-----+
         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-13 15:59

    Trick to such scenarios is to use the Groups formed in CollectionViewSource as the ItemsSource of a DataGrid. And, use a DataGrid itself as the CellTemplate of Column.

    Xaml

        
            
                
                    
                
            
        
    
        
                        
                
                    
                        
                                                       
                                
                                    
                                
                            
                        
                    
                    
                        
                            
                                
                                    
                                        
                                            
                                                
                                                    
                                                
                                            
                                        
                                    
                                
                            
                        
                
                          
            
        
    
    

    DataGrid.Loaded event

     private void dg_Loaded(object sender, RoutedEventArgs e)
        {
            var groups = (this.Resources["CvsKey"] as CollectionViewSource).View.Groups;
            dg.ItemsSource = groups;
        }
    

    This should get you started.

    Output :

提交回复
热议问题