【问题标题】:WP8 StackPanel ControlsWP8 StackPanel 控件
【发布时间】:2013-09-30 19:58:59
【问题描述】:

我有几个TextBlock,我希望将它们组合在一起,这样如果其中一个恰好包含大量文本,它下面的控件就会相应地向下移动。

目前我已经将TextBlock 包裹在<StackPanel> 中,但是,控件现在被广泛地垂直分离。我怎样才能缩短它们之间的距离?

这是我的代码:

<StackPanel>
    <TextBlock TextAlignment="Left" x:Name="lblCategoryTitle" TextWrapping="Wrap" Text="Category"
                VerticalAlignment="Top" Height="29" Width="456" Margin="0,59,0,0" FontWeight="Bold">
        <TextBlock.Foreground>
            <SolidColorBrush Color="{StaticResource PhoneAccentColor}" />
        </TextBlock.Foreground>
    </TextBlock>
    <TextBlock Height="auto" TextAlignment="Left" x:Name="lblCategory" TextWrapping="Wrap"
                Text="Category" Margin="0,88,10,428" />
    <TextBlock TextAlignment="Left" x:Name="lblContactDetails" TextWrapping="Wrap"
                Text="Contact Details" Margin="0,179,289,396" FontWeight="Bold">
        <TextBlock.Foreground>
            <SolidColorBrush Color="{StaticResource PhoneAccentColor}" />
        </TextBlock.Foreground>
    </TextBlock>
    <TextBlock Height="auto" TextAlignment="Left" x:Name="lblName" TextWrapping="Wrap" Text="Name"
                Margin="0,211,10,364" />
    <TextBlock Height="auto" TextAlignment="Left" x:Name="lblAddress" TextWrapping="Wrap"
                Text="Address" Margin="0,243,10,332" />
    <TextBlock Height="auto" TextAlignment="Left" x:Name="lblEmail" TextWrapping="Wrap" Text="Email"
                Margin="0,275,10,300" />
    <TextBlock Height="auto" TextAlignment="Left" x:Name="lblTelephone" TextWrapping="Wrap"
                Text="Telephone" Margin="0,307,10,268" />
</StackPanel>

我想我还应该问一下StackPanel 是否适合我的要求。大家觉得呢?

【问题讨论】:

  • 你的页边距太大了,它们把控件分开了,已经不是绝对定位了。大多数情况下只需使用Margin="12,0,0,12"

标签: xaml windows-phone-8 windows-phone


【解决方案1】:

你有一些相当大的边距 - 如果你减少边距的大小,文本块会更接近。您不需要将所有内容都偏移到顶部(实际上,您不应该),因为堆栈面板将处理与 &lt;StackPanel&gt; 中的前一项的偏移量

【讨论】:

  • 啊,愚蠢的我,我忘记了那些边距。非常感谢罗兰。
【解决方案2】:

如果您不希望您的控件被广泛分开,只需减少边距即可。更改面板类型后,您好像忘记调整边距了。

例如,对于您的最后一个 TextBlock:

<TextBlock Height="auto" TextAlignment="Left" x:Name="lblTelephone" TextWrapping="Wrap"
            Text="Telephone" Margin="0,307,10,268" />

页边距的四个数字分别表示左边距、上、右、下。所以只要减少第二个数字就可以了。

【讨论】:

    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多