2012-03-21 122 views
2

我有一個包含子StackPanels(在代碼後面添加)的StackPanel。StackPanel中的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); 

     } 

當我運行應用程序,我得到以下顯示

page view

黑匣子是父的StackPanel。紅色正方形是我動態添加的子StackPanel。

我該如何讓孩子垂直居中,並調整應用窗口大小。

我應該使用StackPanel還是有另一個面板更適合?

非常感謝

+1

你在第一次嘗試的Horizo​​ntalAlignment = 「中心」? – Paparazzi 2012-03-21 11:38:31

回答

3

放在一個格你的父母的StackPanel和改變的StackPanel的Horizo​​ntalAlignment以中心。

XAML:

<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3"> 
    <StackPanel Name="spDaysWeather" HorizontalAlignment="Center" Orientation="Horizontal"> 
    </StackPanel> 
</Grid> 

如果只是設置了StackPanel中的Horizo​​ntalAlignment以中心還應該工作。

XAML:

<StackPanel Name="spDaysWeather" 
      Grid.Column="0" 
      Grid.ColumnSpan="2" 
      HorizontalAlignment="Center" 
      Grid.Row="3" 
      Orientation="Horizontal"/> 
+0

謝謝:)那個工作! – 2012-03-21 11:43:32