繁体   English   中英

如何自定义列表框的ItemsPanelTemplate的布局?

[英]How do I customize the layout of a ListBox's ItemsPanelTemplate?

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

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

编辑:忘了把我已经尝试过的

我试过了代码

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

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

应该没有那么困难,但这要取决于-如果您对每个项目使用网格或类似的控件,并且您不介意列的宽度不相同且内部具有可变宽度的项目,那么只需添加网格即可到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"

这是我想寻找的一个简单示例,它具有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM