如何在旋转时更改stackpanel宽度

时间:2010-11-10 02:02:24

标签: windows-phone-7

我正在尝试在堆叠面板中显示推文列表,我希望消息包含在可用的屏幕宽度内(横向和纵向)

我将以下stackpanel放在listbox itemtemplates datatemplate

<StackPanel>
<Image Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" />
<StackPanel Width="380">
<TextBlock Text="{Binding UserName}" Foreground="#FFC8AB14" FontSize="28"/>
<TextBlock Text="{Binding Message}" TextWrapping="Wrap" FontSize="20" />
<TextBlock Text="{Binding CreatedAt}" FontSize="12" Foreground="#FFC8AB14" />
</StackPanel>
</StackPanel>

当我将设备旋转到横向时,覆盖推文的堆栈面仍保持宽度= 380,如果我删除宽度,则消息文本块文本不再包装..

我是否需要保持堆叠面板上的宽度并在设备旋转时故意更改它,或者在某种程度上我可以使其适合屏幕宽度并包裹到消息文本?

2 个答案:

答案 0 :(得分:2)

尝试使用Grid而不是StackPanel作为模板中的外部容器。这样,您可以定义要扩展的列以占用所有可用空间。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Image Grid.Column="0" Source="{Binding ImageSource}" Height="73" Width="73" VerticalAlignment="Top" />
    <StackPanel Grid.Column="1">
        <TextBlock Text="{Binding UserName}" Foreground="#FFC8AB14" FontSize="28"/>
        <TextBlock Text="{Binding Message}" FontSize="20" />
        <TextBlock Text="{Binding CreatedAt}" FontSize="12" Foreground="#FFC8AB14" />
    </StackPanel>
</Grid>

答案 1 :(得分:0)

您必须附加到OrientationChanged事件

    public MainPage()
    {
        InitializeComponent();
        this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
    }

    void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
    {
        myStackpanel.Width = 100;
    }