gpt4 book ai didi

c# - StackPanel 内的 StackPanel 对齐方式不正确

转载 作者:行者123 更新时间:2023-11-30 22:30:12 25 4
gpt4 key购买 nike

我有一个包含子 StackPanel 的 StackPanel(在后面的代码中添加)。

父堆栈面板

    <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);

}

当我运行应用程序时,我得到以下显示

page view

黑盒是父 StackPanel。红色方 block 是我正在动态添加的子 StackPanel。

如何让子项垂直居中,并在应用程序窗口改变大小时调整大小。

我应该使用 StackPanel 还是有其他更适合的面板?

非常感谢

最佳答案

将您的父 StackPanel 放在一个 Grid 中,并将 StackPanel 的 Horizo​​ntalAlignment 更改为 Center。

XAML:

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

如果您仅将 StackPanel 的 Horizo​​ntalAlignment 设置为 Center,它也应该有效。

XAML:

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

关于c# - StackPanel 内的 StackPanel 对齐方式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com