StackPanel中的文本不会换行(wp7)

时间:2011-12-01 07:52:40

标签: silverlight windows-phone-7 grid stackpanel

我有文字换行的问题。没有StackPanel这个TextBlock可以工作,但我需要在文本之前放一些小图片。另外,我没有两列(前三行只需要一列)

<ListBox.ItemTemplate>
    <DataTemplate>  
        <Grid>
             <Grid.RowDefinitions >
                 <RowDefinition Height="60"/>
                 <RowDefinition Height="170"/>
                 <RowDefinition Height="50"/>
                 <RowDefinition Height="Auto"/>
                 <RowDefinition Height="Auto"/>
                 <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

             <TextBlock Grid.Row="0"></TextBlock>                                   
             <TextBlock Grid.Row="1"></TextBlock>                                   
             <TextBlock Grid.Row="2"></TextBlock>                                   
             <StackPanel Grid.Row="3" Orientation="Horizontal">
                  <Image  Source="Picture.png" MaxHeight="20" MaxWidth="40" HorizontalAlignment="Center" Margin="0,20,0,0" />
                  <TextBlock Text="Long long long text from Binding" FontSize="25" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Bottom" Padding="20,10,0,0"  />
             </StackPanel>
         </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

1 个答案:

答案 0 :(得分:2)

StackPanel将根据Orientation为其组件提供无限高度或宽度。

如果我查看你的XAML,我会建议在网格中使用两列,并将图像放在左侧:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="40" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions >
                <RowDefinition Height="60"/>
                <RowDefinition Height="170"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Grid.ColumnSpan="2"></TextBlock>
            <TextBlock Grid.Row="1" Grid.ColumnSpan="2"></TextBlock>
            <TextBlock Grid.Row="2" Grid.ColumnSpan="2"></TextBlock>
            <Image Grid.Row="3" Source="Picture.png" MaxHeight="20" HorizontalAlignment="Center" Margin="0,20,0,0" />
            <TextBlock Grid.Column="1" Grid.Row="3" Text="Long long long text from Binding" FontSize="25" 
                        HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" 
                        VerticalAlignment="Bottom" Padding="20,10,0,0"  />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

注意第一个文本框上的Grid.ColumnSpan,这将跨越网格的整个宽度,而不仅仅是第一列。