简体   繁体   English

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

[英]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? 如何修改ListBoxItemsPanelTemplate以自定义显示的列?

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.

相关问题 如何为列表框创建不同的ItemsPanelTemplate - How to create different ItemsPanelTemplate for a ListBox 如何自定义 WPF 状态栏布局? - How do I customize the WPF StatusBar layout? 具有自定义datatemplate和ItemsPanelTemplate时如何设置Listbox背景颜色 - How to set Listbox background color, when it has custom datatemplate and ItemsPanelTemplate 如何将 wpf ListBox 的 ItemTemplate 嵌入到 Window 的资源中? - How do I embed the ItemTemplate for a wpf ListBox into the Window's resources? 如何将单个控件实例添加到ItemsPanelTemplate? - How can I add a single control instance to an ItemsPanelTemplate? 更改ListBox ItemsPanelTemplate惹上了麻烦? - Changing ListBox ItemsPanelTemplate has gotten me into trouble? 使用 ListBox 和 StackPanel 的所有可用空间作为 ItemsPanelTemplate - Use all available space with ListBox and a StackPanel as ItemsPanelTemplate WPF使用ItemsPanelTemplate样式选择列表框的选定项目 - WPF Style Selected Items of ListBox Using an ItemsPanelTemplate 使用Canvas作为ItemsPanelTemplate选择绑定的ListBox项目 - Selecting bound ListBox items with Canvas as ItemsPanelTemplate WPF:将UIElements添加到其ItemsPanelTemplate是画布的ListBox? - WPF: adding UIElements to a ListBox whose ItemsPanelTemplate is a canvas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM