我有一个包含子StackPanels的StackPanel (在后面的代码中添加)。
父StackPanel
<StackPanel Name="spDaysWeather" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.Row="3" Orientation="Horizontal">
</StackPanel>子StackPanels
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吗?或者是否有其他更适合的面板?
非常感谢
发布于 2012-03-21 19:38:10
将您的父StackPanel放在网格中,并将StackPanel的HorizontalAlignment更改为Center。
XAML:
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3">
<StackPanel Name="spDaysWeather" HorizontalAlignment="Center" Orientation="Horizontal">
</StackPanel>
</Grid>如果您仅将StackPanel的HorizontalAlignment设置为Center,它也应该起作用。
XAML:
<StackPanel Name="spDaysWeather"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Grid.Row="3"
Orientation="Horizontal"/>https://stackoverflow.com/questions/9803731
复制相似问题