列表框:动态更改列表项或每行高度

本文关键字:列表 高度 动态 | 更新日期: 2023-09-27 17:59:25

我在Windows Phone 8上工作,我有一个列表框,里面有4个项目。

我想动态更改列表项或每行的高度。

如何做到这一点?

以下是我的代码:

<ListBox Name="TopicListbox"
                     ItemTemplate="{StaticResource TitleDataTemplate}"
                     HorizontalAlignment="Stretch"
                     SelectionChanged="TopicListboxSelectionChanged">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
</ListBox>
<DataTemplate x:Key="TitleDataTemplate">
            <Grid MinHeight="90" 
              HorizontalAlignment="Stretch"
              VerticalAlignment="Center">
            </Grid>
</DataTemplate>

列表框:动态更改列表项或每行高度

保持物品高度同步的一个好方法是使用GridShareSizedScope:

<ListBox Name="TopicListbox" 
                     ItemTemplate="{StaticResource TitleDataTemplate}"
                     HorizontalAlignment="Stretch"
                     SelectionChanged="TopicListboxSelectionChanged"
                     Grid.IsSharedSizeScope="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
</ListBox>
<DataTemplate x:Key="TitleDataTemplate">
            <Grid MinHeight="90" 
              HorizontalAlignment="Stretch"
              VerticalAlignment="Center">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" SharedSizeGroup="A" />
                        </Grid.RowDefinitions>
                        <!-- Your template here -->
            </Grid>
</DataTemplate>

注意添加了Grid.IsSharedSizeScope="True"和带有SharedSizeGroupRowDefinition

这将允许您的物品保持相同的高度,因为它们共享一个高度组