我在文字换行方面遇到了问题。如果没有 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>
最佳答案
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,这将跨越整个网格宽度,而不仅仅是第一列。
关于silverlight - StackPanel 中的文本不换行 (wp7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8338044/