Using WrapPanel and ScrollViewer to give a multi-column Listbox in WPF

前端 未结 2 728
独厮守ぢ
独厮守ぢ 2021-01-01 03:52

I’m making a simple LOB app which loads data from an XML file and displays it in a list with a few buttons for editing.

In my first attempt, everything was ok except

2条回答
  •  情深已故
    2021-01-01 04:16

    It seems like you were on the right track: replacing the ItemsPanelTemplate in the ListBox with a WrapPanel, setting WrapPanel's Orientation to Vertical, and setting ScrollViewer.VerticalScrollBar to Disabled should be all you need to do.

    This works for me:

    
        
            
                
                    
                        
                    
                
                
                    
                
                
                    
                
                
                    
                
                
                    
                
                
                    
                
                
                    
                
                
                    
                
            
        
    
    

    That should cause it to render out a full column vertically, wrap, and then continue on the next column, scrolling as necessary horizontally (but not vertically), as in the picture:

    The key things in this implementation are

    1. Setting Orientation="Vertical" on the WrapPanel so that things wrap vertically and not horizontally, and
    2. Setting ScrollViewer.VerticalScrollBarVisibility="Disabled" on the ListBox so that the ScrollViewer knows to restrict its height to the available space.

提交回复
热议问题