WPF中ListBox 横排的方法+ScrollView
如果只是单纯的让ListBox可以横向配列,这样很简单,只需要更改ListBox的ItemsPanel模板就可以,例如:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
但是这样的修改,ListBox只能横向排列,不会根据宽度自动换行,如果想要横向排列的ListBox支持根据宽度自动换行的话,需要这样写:
<ListBox>
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
如果只是单纯的让ListBox可以横向配列,这样很简单,只需要更改ListBox的ItemsPanel模板就可以,例如:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
但是这样的修改,ListBox只能横向排列,不会根据宽度自动换行,如果想要横向排列的ListBox支持根据宽度自动换行的话,需要这样写:
<ListBox>
<ListBox.Template>
<ControlTemplate TargetType="{x:Type ListBox}">
<WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
</ControlTemplate>
</ListBox.Template>
</ListBox>
如果要再加个垂直的滚动条:请有ScrollView!!!
本文介绍了如何在WPF中让ListBox实现横向排列,并支持根据宽度自动换行,同时加入垂直滚动条的功能。通过修改ItemsPanel模板和使用ScrollView组件,实现了更灵活的布局管理。
440

被折叠的 条评论
为什么被折叠?



