[英]How do I customize the layout of a ListBox's ItemsPanelTemplate?
I'd like to specify multiple columns for my ListBox
, but my googling skills have failed me on this one. 我想为
ListBox
指定多个列,但是我的谷歌搜索技能使我在这一列上失败了。
How can I modify the ItemsPanelTemplate
of a ListBox
to customize the columns displayed? 如何修改
ListBox
的ItemsPanelTemplate
以自定义显示的列?
Edit: forgot to put what I'd tried already 编辑:忘了把我已经尝试过的
I've tried the code 我试过了代码
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
Which works except I lose the vertical scrollbar 除了我失去了垂直滚动条之外,哪个有效
It shoudln't be that difficult but it depends - if you are using a grid or some-such control for each item and you don't mind the columns not being the same width with variable width items inside them, then just adding a grid to the ItemTemplate will do. 应该没有那么困难,但这要取决于-如果您对每个项目使用网格或类似的控件,并且您不介意列的宽度不相同且内部具有可变宽度的项目,那么只需添加网格即可到ItemTemplate即可。
<ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition>
<ColumnDefinition>
<ColumnDefinition>
</Grid.ColumnDefinitions>
<SomeControl Grid.Column="0" />
<SomeControl Grid.Column="1" />
<SomeControl Grid.Column="2" />
</Grid>
</DataTemplate>
</ItemTemplate>
The only problem is if you want the grid columns to all be the same size with variable sized content - in which case it would be a little more involved - otherwise you can either explicitly set a size, or let the content decide the size by setting the column widths to "Auto"
. 唯一的问题是,如果您希望网格列的大小都相同且内容大小可变-在这种情况下会涉及更多内容-否则您可以显式设置大小,或者让内容通过设置来决定大小列宽为
"Auto"
。
Here's a simple example of what I think you're looking for, with 3 columns, item wrapping, and automatic vertical scrolling which will work depending on the surrounding layout. 这是我想寻找的一个简单示例,它具有3列,项目包装和自动垂直滚动,这些滚动条将根据周围的布局而工作。
<ListBox HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border MinHeight="150" Margin="5" Background="Green" CornerRadius="4">
<TextBlock Text="{Binding}" Foreground="White"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
<System:String>One</System:String>
<System:String>Two</System:String>
<System:String>Three</System:String>
<System:String>Four</System:String>
<System:String>Five</System:String>
<System:String>Six</System:String>
<System:String>Seven</System:String>
<System:String>Eight</System:String>
<System:String>Nine</System:String>
<System:String>Ten</System:String>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.