【问题标题】:overflowing textblock in windows phonewindows phone 中的文本块溢出
【发布时间】:2013-10-01 11:55:18
【问题描述】:

我有以下用于堆栈面板的 XAML 代码,它将图像放在左侧,将文本块放在右侧

<StackPanel Height="Auto" HorizontalAlignment="Stretch" Margin="12,0" Name="stackPanel1" VerticalAlignment="Top" Width="Auto" Grid.Row="1" Orientation="Horizontal" Background="White">           
        <Image Height="240" Name="image1" Stretch="Fill" Width="240" HorizontalAlignment="Left" />
        <Grid Height="Auto" Name="grid1" Width="Auto">
            <TextBlock Height="Auto" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBlock1" Text="CVC for strict action on tax evasions, black money" VerticalAlignment="Top" Width="Auto" Foreground="Black" FontSize="24" TextWrapping="Wrap" />
        </Grid>
    </StackPanel>

这会在文本块中产生溢出的文本,超出边界。我想要一个堆栈面板,这样在更改图像大小时,文本块会相应地调整大小,并且总堆栈面板始终保持拉伸状态。

编辑: 整个xaml的整个代码是:

<phone:PhoneApplicationPage 
    x:Class="PanelFullStretch30_9_19_02.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
    d:DataContext="{d:DesignData SampleData/MainViewModelSampleData.xaml}"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <!--Pivot Control-->
        <controls:Pivot Title="MY APPLICATION" Background="Gainsboro" Foreground="Black">
            <!--Pivot item one-->
            <controls:PivotItem Header="first">
                <ListBox x:Name="ExampleBox">

                </ListBox>
            </controls:PivotItem>

            <!--Pivot item two-->
            <controls:PivotItem Header="second">
                <!--Triple line list no text wrapping-->
                <ListBox x:Name="SecondListBox">

                    <!-- Pic on left stackpanel design-->

                    <StackPanel Height="Auto" HorizontalAlignment="Stretch" Margin="12,0" Name="stackPanel1" VerticalAlignment="Top" Width="Auto" Grid.Row="1" Orientation="Horizontal" Background="White">
                        <Image Height="240" Name="image1" Stretch="Fill" Width="240" HorizontalAlignment="Left" />
                        <Grid Height="Auto" Name="grid1" Width="Auto">
                            <TextBlock Height="Auto" HorizontalAlignment="Left" Margin="6,6,0,0" Name="textBlock1" Text="CVC for strict action on tax evasions, black money" VerticalAlignment="Top" Width="Auto" Foreground="Black" FontSize="24" TextWrapping="Wrap" />
                        </Grid>
                    </StackPanel>
                </ListBox>
            </controls:PivotItem>
        </controls:Pivot>

    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

【问题讨论】:

  • 我不确定你在问什么。如果您希望两个控件自动调整大小以保持在同一条线上,您应该使用网格。堆栈面板旨在自动将控件放在新行上,以便它们可以扩展至全宽
  • @KooKiz grid 在列表框内使用时不起作用
  • 您可以从视图中发布您的所有代码吗?

标签: c# xaml windows-phone-7


【解决方案1】:

只要您需要自适应布局,请使用Grid。例如,以下 XAML 将生成一个包含两列的 Grid:左边会自动调整大小,而第二个会填充剩余空间:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
</Grid>

将图片放在第一列,将文本放在第二列。这将确保您想要的布局。

【讨论】:

  • “不起作用”是什么意思?假设您的 XAML 的其余部分已正确设置,这应该可以工作...
  • @ZombieSheep 在列表框内,文本块仍然溢出……不过,没有它似乎也能工作。
  • 对不起,伙计们......我的错误,将第二列定义也设为自动。现在效果很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2015-03-22
  • 1970-01-01
相关资源
最近更新 更多