How to setup a grid as template for an Items control?

前端 未结 2 1793
北海茫月
北海茫月 2021-02-07 03:55

I\'m trying to create an ItemsControl that uses a grid as its ItemsPanel in such a way that it has two columns, where the first columns width is the wi

2条回答
  •  情深已故
    2021-02-07 04:25

    There are multiple problems here for an ItemsControl:

    • Getting your first column to match the width of the largest item
    • Generating a dynamic number of rows
    • Generating more than one item for each iteration of the ItemsControl

    The last one is really the biggest problem, because an ItemsControl wraps each ItemTemplate in a ContentPresenter, so there is no default way of creating more than one item in the panel for each Iteration of the ItemsControl. Your end result would look like this:

    
        ...
    
        
            
        
            
        
            
     
    

    My best suggestion would be to create an ItemTemplate that contains a 1x2 Grid, and use Grid.IsSharedSizeScope to make the width of the first column shared. (The ItemsPanelTemplate would remain the default StackPanel.)

    This way, the end result would look like this:

    
        
            
                
                    
                    
                
                
        
        
            
                
                    
                    
                
                
        
        ...
     
    

提交回复
热议问题