【发布时间】:2012-03-21 11:29:07
【问题描述】:
我有一个 StackPanel,其中包含子 StackPanel(在后面的代码中添加)。
父堆栈面板
<StackPanel Name="spDaysWeather" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.Row="3" Orientation="Horizontal">
</StackPanel>
子 StackPanel
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 还是有其他更适合的面板?
非常感谢
【问题讨论】:
-
你第一次尝试 HorizontalAlignment="Center" 吗?
标签: c# wpf stackpanel