0

我有以下用于堆栈面板的 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>
4

1 回答 1

1

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

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

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

于 2013-10-01T12:54:36.570 回答