C# StackPanel内的StackPanel对齐不正确
我有一个StackPanel,其中包含子StackPanel(在代码隐藏中添加) 父堆栈面板C# StackPanel内的StackPanel对齐不正确,c#,wpf,stackpanel,C#,Wpf,Stackpanel,我有一个StackPanel,其中包含子StackPanel(在代码隐藏中添加) 父堆栈面板 <StackPanel Name="spDaysWeather" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.Row="3" Orientation="Horizontal"> </StackPanel> 子堆叠面板 for (int i=1;i<
<StackPanel Name="spDaysWeather" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.Row="3" Orientation="Horizontal">
</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);
}
for(int i=1;i将父StackPanel置于网格中,并将StackPanel的水平对齐方式更改为居中
XAML:
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3">
<StackPanel Name="spDaysWeather" HorizontalAlignment="Center" Orientation="Horizontal">
</StackPanel>
</Grid>
<StackPanel Name="spDaysWeather"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Grid.Row="3"
Orientation="Horizontal"/>
如果仅将StackPanel的水平对齐设置为“中心”,则该选项也会起作用
XAML:
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3">
<StackPanel Name="spDaysWeather" HorizontalAlignment="Center" Orientation="Horizontal">
</StackPanel>
</Grid>
<StackPanel Name="spDaysWeather"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Grid.Row="3"
Orientation="Horizontal"/>
您第一次尝试HorizontalAlignment=“Center”了吗?