ListView column auto sizing

前端 未结 8 1949
执念已碎
执念已碎 2020-12-18 23:28

Let\'s say I have the following ListView:


  
    
            


        
8条回答
  •  一整个雨季
    2020-12-19 00:10

    This works for me, toggling the Width to ActualWidth and then back to NaN for any columns that don't have widths explicitly set. This will only work if the listview columns do not contain controls. I usually call this after data in the list has changed.

    Public Shared Sub AutoResizeListView(lst As Windows.Controls.ListView)
        Dim gv = DirectCast(lst.View, Windows.Controls.GridView)
        For Each gvc In gv.Columns
            If Double.IsNaN(gvc.Width) Then
                gvc.Width = gvc.ActualWidth
                gvc.Width = Double.NaN
            End If
        Next
    End Sub
    

提交回复
热议问题