c# - Windows Phone 的 silverlight 中的文本换行问题

标签 c# silverlight windows-phone-7 listbox word-wrap

我在为 Windows Phone 7 制作的应用程序的列表框中有一个 twitter feed。我遇到的问题是推文的文本被从列表框的边缘切掉而不是换行绕到一个新行,如下所示:

列表框位于全景图中,工作正常。这是我的代码:

<ListBox x:Name="cheapyListBox" Height="500" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Left" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="132" Tap="Message_OnTap">
                <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                <StackPanel Width="Auto">
                    <!--<TextBlock Text="{Binding UserName}" FontSize="28" Margin="12,0,0,0" /> -->
                    <TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" />
                    <TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="20" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
 </ListBox>

如何让推文文本自动换行而不是被截断?谢谢。

最佳答案

由于内部 StackPanel 嵌套在水平 Stackpanel 中,因此它没有深度限制,因此随着文本变长,TextBlock 会无限扩展。

解决此问题的方法有多种,但一种简单的方法(如果您知道所需的宽度)是将内部 StackPanel 的宽度设置为有限的数字。

例如:

<ListBox x:Name="cheapyListBox" Height="500" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="400" ScrollViewer.VerticalScrollBarVisibility="Hidden" HorizontalContentAlignment="Left" >

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132" Tap="Message_OnTap">
                    <Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                    <StackPanel Width="400">
                        <!--<TextBlock Text="{Binding UserName}" FontSize="28" Margin="12,0,0,0" /> -->
                        <TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="24" />
                        <TextBlock Text="{Binding Date}" TextWrapping="Wrap" FontSize="20" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

希望这有帮助!

关于c# - Windows Phone 的 silverlight 中的文本换行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10628846/

相关文章:

c# - SQL 服务器 : randomize rows (shuffle IDs)

.net - Silverlight:TargetType 的属性类型无效 ="{x:Type TextBlock}"

wpf - 在 Silverlight 4 中以编程方式设置控件的样式?

windows-phone-7 - 在 WP7 中将 azure blob 的内容类型设置为图像

javascript Windows Phone 7.1

silverlight - wp7 列表框弹跳 - 列表底部的项目

c# - 在 ASP.NET Blazor 中获取和发布

c# - Nancy.Authentication.Forms 中没有记住登录信息?

c# - 如何在 Wheelcollider 位置 Unity 制作 Wheel Mesh Renderer

c# - Datagrid 不刷新更改的数据