簡體   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