WPF重写了ListView的ItemsPanel,改用WrapPanel做容器。不能自动换行问题
直接上正确代码:
1 <ListView x:Name="lv_product" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Products}" 2 ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Visible" 3 BorderBrush="Transparent" Background="Transparent"> 4 5 <ListView.ItemsPanel> 6 <ItemsPanelTemplate> 7 <WrapPanel Orientation="Horizontal" FlowDirection="LeftToRight" ></WrapPanel> 8 </ItemsPanelTemplate> 9 </ListView.ItemsPanel> 10 <ListView.ItemTemplate> 11 <DataTemplate> 12 <StackPanel> 13 <Image Source="{Binding PicturePath}" Width="80" Height="80"/> 14 <TextBlock Text="{Binding ProductName}" HorizontalAlignment="Center"/> 15 </StackPanel> 16 </DataTemplate> 17 </ListView.ItemTemplate> 18 </ListView>
注意上面的 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 这个设置很关键。因为ListView自带横向和纵向滚动条,必须关了自带的滚动条后WrapPanel的滚动才会起作用。