Goofy
Goofy

Reputation: 6128

Listbox: Change list item or each row height dynamically

I am working on Windows Phone 8, i have a Listbox with 4 items in it.

I want to change list item or each row height dynamically.

How to achieve this ?

Below is my code:

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

Upvotes: 1

Views: 1264

Answers (1)

Damascus
Damascus

Reputation: 6651

A good way to keep your items synchronized in height is to use a Grid with a ShareSizedScope :

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

Notice the add of Grid.IsSharedSizeScope="True" and of a RowDefinition with a SharedSizeGroup

This will allow your items to all keep the same height as they share a height group

Upvotes: 2

Related Questions