Windows Phone ListBox item height

2019-07-31 04:48发布

问题:

I've searched a lot for an answer and couldn't find one.
So: I'm making a Windows Phone app (targeting 7.1) and I have a ListBox with the following ItemTemplate

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <TextBlock x:Name="subjectName" Text="{Binding Path=name}" TextWrapping="Wrap" Foreground="White" FontSize="30" FontFamily="Segoe WP Semibold" Width="440" Height="99"/>
            <TextBlock x:Name="subjectCode" Text="{Binding Path=code}" FontSize="25" Width="107" HorizontalAlignment="Right" Margin="0,79,0,-12"/>
            <TextBlock x:Name="totalAbsences" Text="{Binding Path=absences}" FontSize="30" Width="107" HorizontalAlignment="Left" Margin="0,119,0,-65"/>
            <TextBlock x:Name="totalGrade" Text="{Binding Path=grades}" FontSize="30" Width="186" HorizontalAlignment="Right" Margin="0,119,0,-62" TextAlignment="Right"/>
            <Rectangle Fill="White" Height="3" Margin="0,0,0,-233" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

And this is what happens:

http://d.pr/i/liBK (can't post images here yet)

So what can be done so that the item's height is adjusted automatically?

回答1:

Since each item in the listbox is enclosed in a Grid, you can specify Grid.RowDefinitions and assign each of the children in the Grid to have a certain Row number.

Sample:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition> <!-- Gets whatever height is necessary -->
        <RowDefinition Height="140"></RowDefinition> <!-- Specific Height -->
        <RowDefinition MaxHeight="*"></RowDefinition> <!-- Fills whatever space is left -->
    </Grid.RowDefinitions>
    <TextBlock x:Name="subjectName" Text="{Binding Path=name}" Grid.Row="0"/>
    <TextBlock x:Name="subjectCode" Text="{Binding Path=code}" Grid.Row="1"/>
    <TextBlock x:Name="totalAbsences" Text="{Binding Path=absences}" Grid.Row="2"/>
</Grid>

I used some of your code and removed some extra properties for the sake of simplicity. Just play around with it and see what works.