代码之家  ›  专栏  ›  技术社区  ›  Bwing

如何在旋转时更改stackpanel宽度

  •  0
  • Bwing  · 技术社区  · 14 年前

    我试图在stackpanel中显示一个tweets列表,我希望消息在可用的屏幕宽度(横向和纵向)内包装

    我把下面的stackpanel放在一个列表框itemtemplates数据模板中

    <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>

    我是否需要保留stackpanel上的宽度,并在设备旋转时故意更改它,或者是否有某种方法可以使它适合屏幕宽度,同时也可以包装成消息文本?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Matt Lacey    14 年前

    尝试使用网格而不是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>
    
        2
  •  0
  •   Greg Buehler    14 年前

    你将不得不附加到 OrientationChanged

        public MainPage()
        {
            InitializeComponent();
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(MainPage_OrientationChanged);
        }
    
        void MainPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            myStackpanel.Width = 100;
        }
    
    推荐文章