ListView column auto sizing

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

Let\'s say I have the following ListView:


  
    
            


        
相关标签:
8条回答
  • 2020-12-19 00:04
    <ListView ScrollViewer.VerticalScrollBarVisibility="Auto" Name="someList">
      <ListView.View>
        <GridView>
          <GridViewColumn Width={Binding ElementName=someList, Path=ActualWidth/3} Header="Something" DisplayMemberBinding="{Binding Path=ShortText}" />
          <GridViewColumn  Width={Binding ElementName=someList, Path=ActualWidth/3} Header="Description" DisplayMemberBinding="{Binding Path=VeryLongTextWithCRs}" />
          <GridViewColumn  Width={Binding ElementName=someList, Path=ActualWidth/3} Header="Something Else" DisplayMemberBinding="{Binding Path=AnotherShortText}" />
        </GridView>
      </ListView.View>
    </ListView>
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题