我有一个包含子StackPanels的StackPanel(后面添加了代码)。
父StackPanel
<StackPanel Name="spDaysWeather" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.Row="3" Orientation="Horizontal">
</StackPanel>
Child 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。红色方块是我动态添加的子StackPanel。
如何让孩子垂直居中,如果应用程序窗口改变大小,也可以调整大小。
我应该使用StackPanel还是有另一个更适合的Panel?
非常感谢
答案 0 :(得分:3)
将您的父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"/>