首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StackPanel中的StackPanel对齐不正确

StackPanel中的StackPanel对齐不正确
EN

Stack Overflow用户
提问于 2012-03-21 19:29:08
回答 1查看 7.6K关注 0票数 2

我有一个包含子StackPanels的StackPanel (在后面的代码中添加)。

父StackPanel

代码语言:javascript
复制
    <StackPanel Name="spDaysWeather"  Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.Row="3" Orientation="Horizontal">



    </StackPanel>

子StackPanels

代码语言:javascript
复制
for (int i=1;i<WeatherList.Count;i++)
        {
            StackPanel StackPanelDay = new StackPanel { Orientation =Orientation.Vertical, VerticalAlignment = VerticalAlignment.Stretch, HorizontalAlignment= HorizontalAlignment.Stretch};
            Label day = new Label { Content = WeatherList[i].weatherdate.DayOfWeek, FontSize = 10 };
            System.Windows.Controls.Image imgweather = new System.Windows.Controls.Image { Source = ReturnCultureImage(String.Format("weather_{0}", WeatherList[i].condition.ToLower().Replace(" ", "_"))), Width = 75, Height = 75};

            StackPanelDay.Children.Add(day);
            StackPanelDay.Children.Add(imgweather);
            spDaysWeather.Children.Add(StackPanelDay);

        }

当我运行这个应用程序时,我得到以下显示

黑盒是父StackPanel。红色方块是我动态添加的子StackPanels。

如何让子项垂直居中,并在应用程序窗口更改大小时调整大小。

我应该使用StackPanel吗?或者是否有其他更适合的面板?

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-21 19:38:10

将您的父StackPanel放在网格中,并将StackPanel的HorizontalAlignment更改为Center。

XAML:

代码语言:javascript
复制
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3">
  <StackPanel Name="spDaysWeather" HorizontalAlignment="Center" Orientation="Horizontal">
  </StackPanel>
</Grid>

如果您仅将StackPanel的HorizontalAlignment设置为Center,它也应该起作用。

XAML:

代码语言:javascript
复制
<StackPanel Name="spDaysWeather" 
            Grid.Column="0"
            Grid.ColumnSpan="2"
            HorizontalAlignment="Center"
            Grid.Row="3" 
            Orientation="Horizontal"/>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9803731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档