0

我想为我的 指定多个列ListBox,但是我的谷歌搜索技能在这一列上失败了。

如何修改ItemsPanelTemplateaListBox以自定义显示的列?

编辑:忘记放我已经尝试过的东西

我试过代码

<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
        <UniformGrid Columns="3" />
    </ItemsPanelTemplate>

除了我失去垂直滚动条之外,哪个有效

4

2 回答 2

0

它不应该那么困难,但这取决于 - 如果您对每个项目使用网格或类似的控件,并且您不介意列的宽度不同且其中包含可变宽度的项目,那么只需添加一个网格到 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>

唯一的问题是,如果您希望网格列的大小都相同且内容大小可变 - 在这种情况下,它会涉及更多一点 - 否则您可以显式设置大小,或者让内容通过设置来决定大小列宽为"Auto"

于 2012-07-11T16:00:23.663 回答
0

这是我认为您正在寻找的一个简单示例,具有 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>
于 2012-07-11T19:14:03.180 回答