StackPanel 中的文本不换行 (wp7)

发布于 2024-12-19 02:43:41 字数 1408 浏览 1 评论 0原文

我的文本换行有问题。如果没有 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>

I have problem with text wrapping. Without StackPanel this TextBlock works, but i need to put small picture before text. Also I don't have two columns for this (i need only one column for first three rows)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-12-26 02:43:41

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,这会将它们跨越网格的整个宽度,而不仅仅是网格的宽度第一栏。

A StackPanel will give its components infinite height or width depending on the Orientation.

If I look at your XAML I would suggest to use two columns in the grid, and put the image on the left side:

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

Notice Grid.ColumnSpan on the first textboxes, this will span them over the entire width of the grid, not just the first column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文